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[] = {
203 end = memchr(buf, '\0', len);
205 return got_error(GOT_ERR_BAD_OBJ_HDR);
207 for (i = 0; i < nitems(obj_labels); i++) {
208 const char *label = obj_labels[i];
209 size_t label_len = strlen(label);
212 if (len <= label_len || buf + label_len >= end ||
213 strncmp(buf, label, label_len) != 0)
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 = end - buf + 1;
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");
252 err = got_inflate_init(&zb, buf, zbsize, NULL);
258 err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
264 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
267 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
268 if (newbuf == NULL) {
269 err = got_error_from_errno("recallocarray");
273 zb.outbuf = newbuf + totlen;
274 zb.outlen = (nbuf * zbsize) - totlen;
276 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
278 err = got_object_parse_header(obj, buf, totlen);
281 got_inflate_end(&zb);
285 struct got_commit_object *
286 got_object_commit_alloc_partial(void)
288 struct got_commit_object *commit;
290 commit = calloc(1, sizeof(*commit));
293 commit->tree_id = malloc(sizeof(*commit->tree_id));
294 if (commit->tree_id == NULL) {
299 STAILQ_INIT(&commit->parent_ids);
304 const struct got_error *
305 got_object_commit_add_parent(struct got_commit_object *commit,
308 const struct got_error *err = NULL;
309 struct got_object_qid *qid;
311 err = got_object_qid_alloc_partial(&qid);
315 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
316 err = got_error(GOT_ERR_BAD_OBJ_DATA);
317 got_object_qid_free(qid);
321 STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
327 static const struct got_error *
328 parse_gmtoff(time_t *gmtoff, const char *tzstr)
331 const char *p = tzstr;
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
341 if (!isdigit(*p) && !isdigit(*(p + 1)))
342 return got_error(GOT_ERR_BAD_OBJ_DATA);
343 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
346 if (!isdigit(*p) && !isdigit(*(p + 1)))
347 return got_error(GOT_ERR_BAD_OBJ_DATA);
348 m = ((*p - '0') * 10) + (*(p + 1) - '0');
350 *gmtoff = (h * 60 * 60 + m * 60) * sign;
354 static const struct got_error *
355 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
357 const struct got_error *err = NULL;
361 /* Parse and strip off trailing timezone indicator string. */
362 space = strrchr(committer, ' ');
364 return got_error(GOT_ERR_BAD_OBJ_DATA);
365 tzstr = strdup(space + 1);
367 return got_error_from_errno("strdup");
368 err = parse_gmtoff(gmtoff, tzstr);
371 if (err->code != GOT_ERR_BAD_OBJ_DATA)
373 /* Old versions of Git omitted the timestamp. */
380 /* Timestamp is separated from committer name + email by space. */
381 space = strrchr(committer, ' ');
383 return got_error(GOT_ERR_BAD_OBJ_DATA);
385 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
386 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
388 return got_error(GOT_ERR_BAD_OBJ_DATA);
390 /* Strip off parsed time information, leaving just author and email. */
397 got_object_commit_close(struct got_commit_object *commit)
399 if (commit->refcnt > 0) {
401 if (commit->refcnt > 0)
405 got_object_id_queue_free(&commit->parent_ids);
406 free(commit->tree_id);
407 free(commit->author);
408 free(commit->committer);
409 free(commit->logmsg);
413 struct got_object_id *
414 got_object_commit_get_tree_id(struct got_commit_object *commit)
416 return commit->tree_id;
420 got_object_commit_get_nparents(struct got_commit_object *commit)
422 return commit->nparents;
425 const struct got_object_id_queue *
426 got_object_commit_get_parent_ids(struct got_commit_object *commit)
428 return &commit->parent_ids;
432 got_object_commit_get_author(struct got_commit_object *commit)
434 return commit->author;
438 got_object_commit_get_author_time(struct got_commit_object *commit)
440 return commit->author_time;
443 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
445 return commit->author_gmtoff;
449 got_object_commit_get_committer(struct got_commit_object *commit)
451 return commit->committer;
455 got_object_commit_get_committer_time(struct got_commit_object *commit)
457 return commit->committer_time;
461 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
463 return commit->committer_gmtoff;
466 const struct got_error *
467 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
469 const struct got_error *err = NULL;
474 len = strlen(commit->logmsg);
475 *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
477 return got_error_from_errno("malloc");
480 * Strip out unusual headers. Headers are separated from the commit
481 * message body by a single empty line.
483 src = commit->logmsg;
485 while (*src != '\0' && *src != '\n') {
486 int copy_header = 1, eol = 0;
487 if (strncmp(src, GOT_COMMIT_LABEL_TREE,
488 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
489 strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
490 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
491 strncmp(src, GOT_COMMIT_LABEL_PARENT,
492 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
493 strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
494 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
497 while (*src != '\0' && !eol) {
509 if (strlcat(*logmsg, src, len + 1) >= len + 1) {
510 err = got_error(GOT_ERR_NO_SPACE);
514 /* Trim redundant trailing whitespace. */
515 len = strlen(*logmsg);
516 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
517 isspace((unsigned char)(*logmsg)[len - 1])) {
518 (*logmsg)[len - 1] = '\0';
522 /* Append a trailing newline if missing. */
523 if (len > 0 && (*logmsg)[len - 1] != '\n') {
524 (*logmsg)[len] = '\n';
525 (*logmsg)[len + 1] = '\0';
536 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
538 return commit->logmsg;
541 const struct got_error *
542 got_object_parse_commit(struct got_commit_object **commit, char *buf,
545 const struct got_error *err = NULL;
548 ssize_t remain = (ssize_t)len;
551 return got_error(GOT_ERR_BAD_OBJ_DATA);
553 *commit = got_object_commit_alloc_partial();
555 return got_error_from_errno("got_object_commit_alloc_partial");
557 label_len = strlen(GOT_COMMIT_LABEL_TREE);
558 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
560 if (remain < SHA1_DIGEST_STRING_LENGTH) {
561 err = got_error(GOT_ERR_BAD_OBJ_DATA);
565 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
566 err = got_error(GOT_ERR_BAD_OBJ_DATA);
569 remain -= SHA1_DIGEST_STRING_LENGTH;
570 s += SHA1_DIGEST_STRING_LENGTH;
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
577 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
579 if (remain < SHA1_DIGEST_STRING_LENGTH) {
580 err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 err = got_object_commit_add_parent(*commit, s);
588 remain -= SHA1_DIGEST_STRING_LENGTH;
589 s += SHA1_DIGEST_STRING_LENGTH;
592 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
593 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
599 err = got_error(GOT_ERR_BAD_OBJ_DATA);
603 p = memchr(s, '\n', remain);
605 err = got_error(GOT_ERR_BAD_OBJ_DATA);
610 err = parse_commit_time(&(*commit)->author_time,
611 &(*commit)->author_gmtoff, s);
614 (*commit)->author = strdup(s);
615 if ((*commit)->author == NULL) {
616 err = got_error_from_errno("strdup");
623 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
624 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
630 err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 p = memchr(s, '\n', remain);
636 err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 err = parse_commit_time(&(*commit)->committer_time,
642 &(*commit)->committer_gmtoff, s);
645 (*commit)->committer = strdup(s);
646 if ((*commit)->committer == NULL) {
647 err = got_error_from_errno("strdup");
654 (*commit)->logmsg = strndup(s, remain);
655 if ((*commit)->logmsg == NULL) {
656 err = got_error_from_errno("strndup");
661 got_object_commit_close(*commit);
668 got_object_tree_close(struct got_tree_object *tree)
670 if (tree->refcnt > 0) {
672 if (tree->refcnt > 0)
680 static const struct got_error *
681 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
682 size_t *elen, char *buf,
686 const struct got_error *err = NULL;
691 *pte = malloc(sizeof(**pte));
693 return got_error_from_errno("malloc");
695 *elen = strnlen(buf, maxlen) + 1;
696 if (*elen > maxlen) {
699 return got_error(GOT_ERR_BAD_OBJ_DATA);
702 space = memchr(buf, ' ', *elen);
703 if (space == NULL || space <= buf) {
704 err = got_error(GOT_ERR_BAD_OBJ_DATA);
712 if (*p < '0' && *p > '7') {
713 err = got_error(GOT_ERR_BAD_OBJ_DATA);
717 (*pte)->mode |= *p - '0';
721 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
722 err = got_error(GOT_ERR_BAD_OBJ_DATA);
728 *elen += SHA1_DIGEST_LENGTH;
737 const struct got_error *
738 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
739 uint8_t *buf, size_t len)
741 const struct got_error *err = NULL;
746 return NULL; /* tree is empty */
749 struct got_parsed_tree_entry *pte;
750 struct got_pathlist_entry *new = NULL;
754 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
757 err = got_pathlist_insert(&new, entries, name, pte);
761 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
770 err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 got_object_parsed_tree_entries_free(entries);
782 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
784 struct got_pathlist_entry *pe;
786 TAILQ_FOREACH(pe, entries, entry) {
787 struct got_parsed_tree_entry *pte = pe->data;
790 got_pathlist_free(entries);
794 got_object_tag_close(struct got_tag_object *tag)
796 if (tag->refcnt > 0) {
808 const struct got_error *
809 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
811 const struct got_error *err = NULL;
817 return got_error(GOT_ERR_BAD_OBJ_DATA);
819 *tag = calloc(1, sizeof(**tag));
821 return got_error_from_errno("calloc");
823 label_len = strlen(GOT_TAG_LABEL_OBJECT);
824 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
826 if (remain < SHA1_DIGEST_STRING_LENGTH) {
827 err = got_error(GOT_ERR_BAD_OBJ_DATA);
831 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
832 err = got_error(GOT_ERR_BAD_OBJ_DATA);
835 remain -= SHA1_DIGEST_STRING_LENGTH;
836 s += SHA1_DIGEST_STRING_LENGTH;
838 err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 err = got_error(GOT_ERR_BAD_OBJ_DATA);
847 label_len = strlen(GOT_TAG_LABEL_TYPE);
848 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
851 err = got_error(GOT_ERR_BAD_OBJ_DATA);
855 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
856 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
857 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
858 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
861 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
862 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
863 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
864 label_len = strlen(GOT_OBJ_LABEL_TREE);
867 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
868 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
869 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
870 label_len = strlen(GOT_OBJ_LABEL_BLOB);
873 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
874 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
875 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
876 label_len = strlen(GOT_OBJ_LABEL_TAG);
880 err = got_error(GOT_ERR_BAD_OBJ_DATA);
884 if (remain <= 0 || *s != '\n') {
885 err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 err = got_error(GOT_ERR_BAD_OBJ_DATA);
895 err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 label_len = strlen(GOT_TAG_LABEL_TAG);
900 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
905 err = got_error(GOT_ERR_BAD_OBJ_DATA);
909 p = memchr(s, '\n', remain);
911 err = got_error(GOT_ERR_BAD_OBJ_DATA);
916 (*tag)->tag = strndup(s, slen);
917 if ((*tag)->tag == NULL) {
918 err = got_error_from_errno("strndup");
924 err = got_error(GOT_ERR_BAD_OBJ_DATA);
928 err = got_error(GOT_ERR_BAD_OBJ_DATA);
932 label_len = strlen(GOT_TAG_LABEL_TAGGER);
933 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
939 err = got_error(GOT_ERR_BAD_OBJ_DATA);
943 p = memchr(s, '\n', remain);
945 err = got_error(GOT_ERR_BAD_OBJ_DATA);
950 err = parse_commit_time(&(*tag)->tagger_time,
951 &(*tag)->tagger_gmtoff, s);
954 (*tag)->tagger = strdup(s);
955 if ((*tag)->tagger == NULL) {
956 err = got_error_from_errno("strdup");
962 err = got_error(GOT_ERR_BAD_OBJ_DATA);
966 /* Some old tags in the Linux git repo have no tagger. */
967 (*tag)->tagger = strdup("");
968 if ((*tag)->tagger == NULL) {
969 err = got_error_from_errno("strdup");
974 (*tag)->tagmsg = strndup(s, remain);
975 if ((*tag)->tagmsg == NULL) {
976 err = got_error_from_errno("strndup");
981 got_object_tag_close(*tag);
987 const struct got_error *
988 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
990 const struct got_error *err = NULL;
991 static const size_t blocksize = 512;
992 size_t n, total, remain;
998 buf = malloc(blocksize);
1000 return got_error_from_errno("malloc");
1007 newbuf = reallocarray(buf, 1, total + blocksize);
1008 if (newbuf == NULL) {
1009 err = got_error_from_errno("reallocarray");
1013 remain += blocksize;
1015 n = fread(buf + total, 1, remain, f);
1018 err = got_ferror(f, GOT_ERR_IO);