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[] = {
205 end = memchr(buf, '\0', len);
207 return got_error(GOT_ERR_BAD_OBJ_HDR);
209 for (i = 0; i < nitems(obj_labels); i++) {
210 const char *label = obj_labels[i];
211 size_t label_len = strlen(label);
214 if (len <= label_len || buf + label_len >= end ||
215 strncmp(buf, label, label_len) != 0)
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 = end - buf + 1;
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");
254 err = got_inflate_init(&zb, buf, zbsize, NULL);
260 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
266 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
269 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
270 if (newbuf == NULL) {
271 err = got_error_from_errno("recallocarray");
275 zb.outbuf = newbuf + totlen;
276 zb.outlen = (nbuf * zbsize) - totlen;
278 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
280 err = got_object_parse_header(obj, buf, totlen);
283 got_inflate_end(&zb);
287 struct got_commit_object *
288 got_object_commit_alloc_partial(void)
290 struct got_commit_object *commit;
292 commit = calloc(1, sizeof(*commit));
295 commit->tree_id = malloc(sizeof(*commit->tree_id));
296 if (commit->tree_id == NULL) {
301 STAILQ_INIT(&commit->parent_ids);
306 const struct got_error *
307 got_object_commit_add_parent(struct got_commit_object *commit,
310 const struct got_error *err = NULL;
311 struct got_object_qid *qid;
313 err = got_object_qid_alloc_partial(&qid);
317 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
318 err = got_error(GOT_ERR_BAD_OBJ_DATA);
319 got_object_qid_free(qid);
323 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
329 static const struct got_error *
330 parse_gmtoff(time_t *gmtoff, const char *tzstr)
333 const char *p = tzstr;
341 return got_error(GOT_ERR_BAD_OBJ_DATA);
343 if (!isdigit(*p) && !isdigit(*(p + 1)))
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
345 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
348 if (!isdigit(*p) && !isdigit(*(p + 1)))
349 return got_error(GOT_ERR_BAD_OBJ_DATA);
350 m = ((*p - '0') * 10) + (*(p + 1) - '0');
352 *gmtoff = (h * 60 * 60 + m * 60) * sign;
356 static const struct got_error *
357 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
359 const struct got_error *err = NULL;
363 /* Parse and strip off trailing timezone indicator string. */
364 space = strrchr(committer, ' ');
366 return got_error(GOT_ERR_BAD_OBJ_DATA);
367 tzstr = strdup(space + 1);
369 return got_error_from_errno("strdup");
370 err = parse_gmtoff(gmtoff, tzstr);
373 if (err->code != GOT_ERR_BAD_OBJ_DATA)
375 /* Old versions of Git omitted the timestamp. */
382 /* Timestamp is separated from committer name + email by space. */
383 space = strrchr(committer, ' ');
385 return got_error(GOT_ERR_BAD_OBJ_DATA);
387 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
388 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
390 return got_error(GOT_ERR_BAD_OBJ_DATA);
392 /* Strip off parsed time information, leaving just author and email. */
399 got_object_commit_close(struct got_commit_object *commit)
401 if (commit->refcnt > 0) {
403 if (commit->refcnt > 0)
407 got_object_id_queue_free(&commit->parent_ids);
408 free(commit->tree_id);
409 free(commit->author);
410 free(commit->committer);
411 free(commit->logmsg);
415 struct got_object_id *
416 got_object_commit_get_tree_id(struct got_commit_object *commit)
418 return commit->tree_id;
422 got_object_commit_get_nparents(struct got_commit_object *commit)
424 return commit->nparents;
427 const struct got_object_id_queue *
428 got_object_commit_get_parent_ids(struct got_commit_object *commit)
430 return &commit->parent_ids;
434 got_object_commit_get_author(struct got_commit_object *commit)
436 return commit->author;
440 got_object_commit_get_author_time(struct got_commit_object *commit)
442 return commit->author_time;
445 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
447 return commit->author_gmtoff;
451 got_object_commit_get_committer(struct got_commit_object *commit)
453 return commit->committer;
457 got_object_commit_get_committer_time(struct got_commit_object *commit)
459 return commit->committer_time;
463 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
465 return commit->committer_gmtoff;
468 const struct got_error *
469 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
471 const struct got_error *err = NULL;
476 len = strlen(commit->logmsg);
477 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
479 return got_error_from_errno("malloc");
482 * Strip out unusual headers. Headers are separated from the commit
483 * message body by a single empty line.
485 src = commit->logmsg;
487 while (*src != '\0' && *src != '\n') {
488 int copy_header = 1, eol = 0;
489 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
490 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
491 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
492 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
493 strncmp(src, GOT_COMMIT_LABEL_PARENT,
494 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
495 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
496 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
499 while (*src != '\0' && !eol) {
511 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
512 err = got_error(GOT_ERR_NO_SPACE);
516 /* Trim redundant trailing whitespace. */
517 len = strlen(*logmsg);
518 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
519 isspace((unsigned char)(*logmsg)[len - 1])) {
520 (*logmsg)[len - 1] = '\0';
524 /* Append a trailing newline if missing. */
525 if (len > 0 && (*logmsg)[len - 1] != '\n') {
526 (*logmsg)[len] = '\n';
527 (*logmsg)[len + 1] = '\0';
538 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
540 return commit->logmsg;
543 const struct got_error *
544 got_object_parse_commit(struct got_commit_object **commit, char *buf,
547 const struct got_error *err = NULL;
550 ssize_t remain = (ssize_t)len;
553 return got_error(GOT_ERR_BAD_OBJ_DATA);
555 *commit = got_object_commit_alloc_partial();
557 return got_error_from_errno("got_object_commit_alloc_partial");
559 label_len = strlen(GOT_COMMIT_LABEL_TREE);
560 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
562 if (remain < SHA1_DIGEST_STRING_LENGTH) {
563 err = got_error(GOT_ERR_BAD_OBJ_DATA);
567 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
568 err = got_error(GOT_ERR_BAD_OBJ_DATA);
571 remain -= SHA1_DIGEST_STRING_LENGTH;
572 s += SHA1_DIGEST_STRING_LENGTH;
574 err = got_error(GOT_ERR_BAD_OBJ_DATA);
578 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
579 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
581 if (remain < SHA1_DIGEST_STRING_LENGTH) {
582 err = got_error(GOT_ERR_BAD_OBJ_DATA);
586 err = got_object_commit_add_parent(*commit, s);
590 remain -= SHA1_DIGEST_STRING_LENGTH;
591 s += SHA1_DIGEST_STRING_LENGTH;
594 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
595 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, 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)->author_time,
613 &(*commit)->author_gmtoff, s);
616 (*commit)->author = strdup(s);
617 if ((*commit)->author == NULL) {
618 err = got_error_from_errno("strdup");
625 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
626 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
632 err = got_error(GOT_ERR_BAD_OBJ_DATA);
636 p = memchr(s, '\n', remain);
638 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 err = parse_commit_time(&(*commit)->committer_time,
644 &(*commit)->committer_gmtoff, s);
647 (*commit)->committer = strdup(s);
648 if ((*commit)->committer == NULL) {
649 err = got_error_from_errno("strdup");
656 (*commit)->logmsg = strndup(s, remain);
657 if ((*commit)->logmsg == NULL) {
658 err = got_error_from_errno("strndup");
663 got_object_commit_close(*commit);
670 got_object_tree_close(struct got_tree_object *tree)
672 if (tree->refcnt > 0) {
674 if (tree->refcnt > 0)
682 static const struct got_error *
683 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
684 size_t *elen, char *buf,
688 const struct got_error *err = NULL;
693 *pte = malloc(sizeof(**pte));
695 return got_error_from_errno("malloc");
697 *elen = strnlen(buf, maxlen) + 1;
698 if (*elen > maxlen) {
701 return got_error(GOT_ERR_BAD_OBJ_DATA);
704 space = memchr(buf, ' ', *elen);
705 if (space == NULL || space <= buf) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
714 if (*p < '0' && *p > '7') {
715 err = got_error(GOT_ERR_BAD_OBJ_DATA);
719 (*pte)->mode |= *p - '0';
723 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
724 err = got_error(GOT_ERR_BAD_OBJ_DATA);
730 *elen += SHA1_DIGEST_LENGTH;
739 const struct got_error *
740 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
741 uint8_t *buf, size_t len)
743 const struct got_error *err = NULL;
748 return NULL; /* tree is empty */
751 struct got_parsed_tree_entry *pte;
752 struct got_pathlist_entry *new = NULL;
756 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
759 err = got_pathlist_insert(&new, entries, name, pte);
763 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
772 err = got_error(GOT_ERR_BAD_OBJ_DATA);
777 got_object_parsed_tree_entries_free(entries);
784 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
786 struct got_pathlist_entry *pe;
788 TAILQ_FOREACH(pe, entries, entry) {
789 struct got_parsed_tree_entry *pte = pe->data;
792 got_pathlist_free(entries);
796 got_object_tag_close(struct got_tag_object *tag)
798 if (tag->refcnt > 0) {
810 const struct got_error *
811 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
813 const struct got_error *err = NULL;
819 return got_error(GOT_ERR_BAD_OBJ_DATA);
821 *tag = calloc(1, sizeof(**tag));
823 return got_error_from_errno("calloc");
825 label_len = strlen(GOT_TAG_LABEL_OBJECT);
826 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
828 if (remain < SHA1_DIGEST_STRING_LENGTH) {
829 err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
834 err = got_error(GOT_ERR_BAD_OBJ_DATA);
837 remain -= SHA1_DIGEST_STRING_LENGTH;
838 s += SHA1_DIGEST_STRING_LENGTH;
840 err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 err = got_error(GOT_ERR_BAD_OBJ_DATA);
849 label_len = strlen(GOT_TAG_LABEL_TYPE);
850 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
853 err = got_error(GOT_ERR_BAD_OBJ_DATA);
857 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
858 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
859 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
860 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
863 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
864 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
865 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
866 label_len = strlen(GOT_OBJ_LABEL_TREE);
869 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
870 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
871 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
872 label_len = strlen(GOT_OBJ_LABEL_BLOB);
875 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
876 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
877 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
878 label_len = strlen(GOT_OBJ_LABEL_TAG);
882 err = got_error(GOT_ERR_BAD_OBJ_DATA);
886 if (remain <= 0 || *s != '\n') {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
893 err = got_error(GOT_ERR_BAD_OBJ_DATA);
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 label_len = strlen(GOT_TAG_LABEL_TAG);
902 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
907 err = got_error(GOT_ERR_BAD_OBJ_DATA);
911 p = memchr(s, '\n', remain);
913 err = got_error(GOT_ERR_BAD_OBJ_DATA);
918 (*tag)->tag = strndup(s, slen);
919 if ((*tag)->tag == NULL) {
920 err = got_error_from_errno("strndup");
926 err = got_error(GOT_ERR_BAD_OBJ_DATA);
930 err = got_error(GOT_ERR_BAD_OBJ_DATA);
934 label_len = strlen(GOT_TAG_LABEL_TAGGER);
935 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
941 err = got_error(GOT_ERR_BAD_OBJ_DATA);
945 p = memchr(s, '\n', remain);
947 err = got_error(GOT_ERR_BAD_OBJ_DATA);
952 err = parse_commit_time(&(*tag)->tagger_time,
953 &(*tag)->tagger_gmtoff, s);
956 (*tag)->tagger = strdup(s);
957 if ((*tag)->tagger == NULL) {
958 err = got_error_from_errno("strdup");
964 err = got_error(GOT_ERR_BAD_OBJ_DATA);
968 /* Some old tags in the Linux git repo have no tagger. */
969 (*tag)->tagger = strdup("");
970 if ((*tag)->tagger == NULL) {
971 err = got_error_from_errno("strdup");
976 (*tag)->tagmsg = strndup(s, remain);
977 if ((*tag)->tagmsg == NULL) {
978 err = got_error_from_errno("strndup");
983 got_object_tag_close(*tag);
989 const struct got_error *
990 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
992 const struct got_error *err = NULL;
993 static const size_t blocksize = 512;
994 size_t n, total, remain;
1000 buf = malloc(blocksize);
1002 return got_error_from_errno("malloc");
1009 newbuf = reallocarray(buf, 1, total + blocksize);
1010 if (newbuf == NULL) {
1011 err = got_error_from_errno("reallocarray");
1015 remain += blocksize;
1017 n = fread(buf + total, 1, remain, f);
1020 err = got_ferror(f, GOT_ERR_IO);