2 * Copyright (c) 2018, 2019 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>
21 #include <sys/socket.h>
22 #include <sys/syslimits.h>
38 #include "got_error.h"
39 #include "got_object.h"
40 #include "got_repository.h"
41 #include "got_opentemp.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
55 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
59 got_object_id_cmp(const struct got_object_id *id1,
60 const struct got_object_id *id2)
62 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
65 const struct got_error *
66 got_object_qid_alloc_partial(struct got_object_qid **qid)
68 const struct got_error *err = NULL;
70 *qid = malloc(sizeof(**qid));
72 return got_error_from_errno("malloc");
74 (*qid)->id = malloc(sizeof(*((*qid)->id)));
75 if ((*qid)->id == NULL) {
76 err = got_error_from_errno("malloc");
77 got_object_qid_free(*qid);
85 const struct got_error *
86 got_object_id_str(char **outbuf, struct got_object_id *id)
88 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
90 *outbuf = malloc(len);
92 return got_error_from_errno("malloc");
94 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
97 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
104 got_object_close(struct got_object *obj)
106 if (obj->refcnt > 0) {
112 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
113 struct got_delta *delta;
114 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
115 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
116 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
124 got_object_qid_free(struct got_object_qid *qid)
131 got_object_id_queue_free(struct got_object_id_queue *ids)
133 struct got_object_qid *qid;
135 while (!SIMPLEQ_EMPTY(ids)) {
136 qid = SIMPLEQ_FIRST(ids);
137 SIMPLEQ_REMOVE_HEAD(ids, entry);
138 got_object_qid_free(qid);
142 const struct got_error *
143 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
145 const char *obj_labels[] = {
146 GOT_OBJ_LABEL_COMMIT,
151 const int obj_types[] = {
158 size_t size = 0, hdrlen = 0;
163 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
165 return got_error(GOT_ERR_BAD_OBJ_HDR);
167 for (i = 0; i < nitems(obj_labels); i++) {
168 const char *label = obj_labels[i];
169 size_t label_len = strlen(label);
172 if (strncmp(buf, label, label_len) != 0)
176 if (len <= label_len)
177 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
180 return got_error(GOT_ERR_BAD_OBJ_HDR);
185 return got_error(GOT_ERR_BAD_OBJ_HDR);
187 *obj = calloc(1, sizeof(**obj));
189 return got_error_from_errno("calloc");
191 (*obj)->hdrlen = hdrlen;
196 const struct got_error *
197 got_object_read_header(struct got_object **obj, int fd)
199 const struct got_error *err;
200 struct got_inflate_buf zb;
202 const size_t zbsize = 64;
203 size_t outlen, totlen;
208 buf = malloc(zbsize);
210 return got_error_from_errno("malloc");
212 err = got_inflate_init(&zb, buf, zbsize);
218 err = got_inflate_read_fd(&zb, fd, &outlen);
224 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
227 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
228 if (newbuf == NULL) {
229 err = got_error_from_errno("recallocarray");
233 zb.outbuf = newbuf + totlen;
234 zb.outlen = (nbuf * zbsize) - totlen;
236 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
238 err = got_object_parse_header(obj, buf, totlen);
241 got_inflate_end(&zb);
245 struct got_commit_object *
246 got_object_commit_alloc_partial(void)
248 struct got_commit_object *commit;
250 commit = calloc(1, sizeof(*commit));
253 commit->tree_id = malloc(sizeof(*commit->tree_id));
254 if (commit->tree_id == NULL) {
259 SIMPLEQ_INIT(&commit->parent_ids);
264 const struct got_error *
265 got_object_commit_add_parent(struct got_commit_object *commit,
268 const struct got_error *err = NULL;
269 struct got_object_qid *qid;
271 err = got_object_qid_alloc_partial(&qid);
275 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
276 err = got_error(GOT_ERR_BAD_OBJ_DATA);
277 got_object_qid_free(qid);
281 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
287 static const struct got_error *
288 parse_gmtoff(time_t *gmtoff, const char *tzstr)
291 const char *p = tzstr;
299 return got_error(GOT_ERR_BAD_OBJ_DATA);
301 if (!isdigit(*p) && !isdigit(*(p + 1)))
302 return got_error(GOT_ERR_BAD_OBJ_DATA);
303 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
306 if (!isdigit(*p) && !isdigit(*(p + 1)))
307 return got_error(GOT_ERR_BAD_OBJ_DATA);
308 m = ((*p - '0') * 10) + (*(p + 1) - '0');
310 *gmtoff = (h * 60 * 60 + m * 60) * sign;
314 static const struct got_error *
315 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
317 const struct got_error *err = NULL;
321 /* Parse and strip off trailing timezone indicator string. */
322 space = strrchr(committer, ' ');
324 return got_error(GOT_ERR_BAD_OBJ_DATA);
325 tzstr = strdup(space + 1);
327 return got_error_from_errno("strdup");
328 err = parse_gmtoff(gmtoff, tzstr);
334 /* Timestamp is separated from committer name + email by space. */
335 space = strrchr(committer, ' ');
337 return got_error(GOT_ERR_BAD_OBJ_DATA);
339 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
340 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
342 return got_error(GOT_ERR_BAD_OBJ_DATA);
344 /* Strip off parsed time information, leaving just author and email. */
351 got_object_commit_close(struct got_commit_object *commit)
353 if (commit->refcnt > 0) {
355 if (commit->refcnt > 0)
359 got_object_id_queue_free(&commit->parent_ids);
360 free(commit->tree_id);
361 free(commit->author);
362 free(commit->committer);
363 free(commit->logmsg);
367 struct got_object_id *
368 got_object_commit_get_tree_id(struct got_commit_object *commit)
370 return commit->tree_id;
374 got_object_commit_get_nparents(struct got_commit_object *commit)
376 return commit->nparents;
379 const struct got_object_id_queue *
380 got_object_commit_get_parent_ids(struct got_commit_object *commit)
382 return &commit->parent_ids;
386 got_object_commit_get_author(struct got_commit_object *commit)
388 return commit->author;
392 got_object_commit_get_author_time(struct got_commit_object *commit)
394 return commit->author_time;
397 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
399 return commit->author_gmtoff;
403 got_object_commit_get_committer(struct got_commit_object *commit)
405 return commit->committer;
409 got_object_commit_get_committer_time(struct got_commit_object *commit)
411 return commit->committer_time;
415 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
417 return commit->committer_gmtoff;
420 const struct got_error *
421 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
423 const struct got_error *err = NULL;
424 char *msg0, *msg, *line, *s;
430 msg0 = strdup(commit->logmsg);
432 return got_error_from_errno("strdup");
434 /* Copy log message line by line to strip out unusual headers... */
437 if ((line = strsep(&msg, "\n")) == NULL)
441 if (line[0] != '\0' &&
442 strncmp(line, GOT_COMMIT_LABEL_TREE,
443 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
444 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
445 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
446 strncmp(line, GOT_COMMIT_LABEL_PARENT,
447 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
448 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
449 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
456 if (asprintf(&s, "%s%s\n",
457 *logmsg ? *logmsg : "", line) == -1) {
458 err = got_error_from_errno("asprintf");
466 /* Trim redundant trailing whitespace. */
467 len = strlen(*logmsg);
468 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
469 isspace((unsigned char)(*logmsg)[len - 1])) {
470 (*logmsg)[len - 1] = '\0';
483 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
485 return commit->logmsg;
488 const struct got_error *
489 got_object_parse_commit(struct got_commit_object **commit, char *buf,
492 const struct got_error *err = NULL;
495 ssize_t remain = (ssize_t)len;
498 return got_error(GOT_ERR_BAD_OBJ_DATA);
500 *commit = got_object_commit_alloc_partial();
502 return got_error_from_errno("got_object_commit_alloc_partial");
504 label_len = strlen(GOT_COMMIT_LABEL_TREE);
505 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
507 if (remain < SHA1_DIGEST_STRING_LENGTH) {
508 err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
513 err = got_error(GOT_ERR_BAD_OBJ_DATA);
516 remain -= SHA1_DIGEST_STRING_LENGTH;
517 s += SHA1_DIGEST_STRING_LENGTH;
519 err = got_error(GOT_ERR_BAD_OBJ_DATA);
523 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
524 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
526 if (remain < SHA1_DIGEST_STRING_LENGTH) {
527 err = got_error(GOT_ERR_BAD_OBJ_DATA);
531 err = got_object_commit_add_parent(*commit, s);
535 remain -= SHA1_DIGEST_STRING_LENGTH;
536 s += SHA1_DIGEST_STRING_LENGTH;
539 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
540 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
546 err = got_error(GOT_ERR_BAD_OBJ_DATA);
550 p = memchr(s, '\n', remain);
552 err = got_error(GOT_ERR_BAD_OBJ_DATA);
557 err = parse_commit_time(&(*commit)->author_time,
558 &(*commit)->author_gmtoff, s);
561 (*commit)->author = strdup(s);
562 if ((*commit)->author == NULL) {
563 err = got_error_from_errno("strdup");
570 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
571 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
577 err = got_error(GOT_ERR_BAD_OBJ_DATA);
581 p = memchr(s, '\n', remain);
583 err = got_error(GOT_ERR_BAD_OBJ_DATA);
588 err = parse_commit_time(&(*commit)->committer_time,
589 &(*commit)->committer_gmtoff, s);
592 (*commit)->committer = strdup(s);
593 if ((*commit)->committer == NULL) {
594 err = got_error_from_errno("strdup");
601 (*commit)->logmsg = strndup(s, remain);
602 if ((*commit)->logmsg == NULL) {
603 err = got_error_from_errno("strndup");
608 got_object_commit_close(*commit);
615 got_object_tree_close(struct got_tree_object *tree)
617 if (tree->refcnt > 0) {
619 if (tree->refcnt > 0)
627 static const struct got_error *
628 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
629 size_t *elen, char *buf,
633 const struct got_error *err = NULL;
638 *pte = malloc(sizeof(**pte));
640 return got_error_from_errno("malloc");
642 *elen = strnlen(buf, maxlen) + 1;
643 if (*elen > maxlen) {
646 return got_error(GOT_ERR_BAD_OBJ_DATA);
649 space = memchr(buf, ' ', *elen);
650 if (space == NULL || space <= buf) {
651 err = got_error(GOT_ERR_BAD_OBJ_DATA);
659 if (*p < '0' && *p > '7') {
660 err = got_error(GOT_ERR_BAD_OBJ_DATA);
664 (*pte)->mode |= *p - '0';
668 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
669 err = got_error(GOT_ERR_BAD_OBJ_DATA);
675 *elen += SHA1_DIGEST_LENGTH;
684 const struct got_error *
685 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
686 uint8_t *buf, size_t len)
688 const struct got_error *err = NULL;
693 return NULL; /* tree is empty */
696 struct got_parsed_tree_entry *pte;
697 struct got_pathlist_entry *new = NULL;
701 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
704 err = got_pathlist_insert(&new, entries, name, pte);
708 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
717 err = got_error(GOT_ERR_BAD_OBJ_DATA);
722 got_pathlist_free(entries);
729 got_object_tag_close(struct got_tag_object *tag)
731 if (tag->refcnt > 0) {
743 const struct got_error *
744 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
746 const struct got_error *err = NULL;
752 return got_error(GOT_ERR_BAD_OBJ_DATA);
754 *tag = calloc(1, sizeof(**tag));
756 return got_error_from_errno("calloc");
758 label_len = strlen(GOT_TAG_LABEL_OBJECT);
759 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
761 if (remain < SHA1_DIGEST_STRING_LENGTH) {
762 err = got_error(GOT_ERR_BAD_OBJ_DATA);
766 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
767 err = got_error(GOT_ERR_BAD_OBJ_DATA);
770 remain -= SHA1_DIGEST_STRING_LENGTH;
771 s += SHA1_DIGEST_STRING_LENGTH;
773 err = got_error(GOT_ERR_BAD_OBJ_DATA);
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
782 label_len = strlen(GOT_TAG_LABEL_TYPE);
783 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
786 err = got_error(GOT_ERR_BAD_OBJ_DATA);
790 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
791 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
792 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
793 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
796 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
797 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
798 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
799 label_len = strlen(GOT_OBJ_LABEL_TREE);
802 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
803 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
804 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
805 label_len = strlen(GOT_OBJ_LABEL_BLOB);
808 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
809 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
810 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
811 label_len = strlen(GOT_OBJ_LABEL_TAG);
815 err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 if (remain <= 0 || *s != '\n') {
820 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 err = got_error(GOT_ERR_BAD_OBJ_DATA);
830 err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 label_len = strlen(GOT_TAG_LABEL_TAG);
835 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
840 err = got_error(GOT_ERR_BAD_OBJ_DATA);
844 p = memchr(s, '\n', remain);
846 err = got_error(GOT_ERR_BAD_OBJ_DATA);
851 (*tag)->tag = strndup(s, slen);
852 if ((*tag)->tag == NULL) {
853 err = got_error_from_errno("strndup");
859 err = got_error(GOT_ERR_BAD_OBJ_DATA);
863 err = got_error(GOT_ERR_BAD_OBJ_DATA);
867 label_len = strlen(GOT_TAG_LABEL_TAGGER);
868 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
874 err = got_error(GOT_ERR_BAD_OBJ_DATA);
878 p = memchr(s, '\n', remain);
880 err = got_error(GOT_ERR_BAD_OBJ_DATA);
885 err = parse_commit_time(&(*tag)->tagger_time,
886 &(*tag)->tagger_gmtoff, s);
889 (*tag)->tagger = strdup(s);
890 if ((*tag)->tagger == NULL) {
891 err = got_error_from_errno("strdup");
897 err = got_error(GOT_ERR_BAD_OBJ_DATA);
901 /* Some old tags in the Linux git repo have no tagger. */
902 (*tag)->tagger = strdup("");
903 if ((*tag)->tagger == NULL) {
904 err = got_error_from_errno("strdup");
909 (*tag)->tagmsg = strndup(s, remain);
910 if ((*tag)->tagmsg == NULL) {
911 err = got_error_from_errno("strndup");
916 got_object_tag_close(*tag);
922 const struct got_error *
923 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
925 const struct got_error *err = NULL;
926 static const size_t blocksize = 512;
927 size_t n, total, remain;
933 buf = malloc(blocksize);
935 return got_error_from_errno("malloc");
942 newbuf = reallocarray(buf, 1, total + blocksize);
943 if (newbuf == NULL) {
944 err = got_error_from_errno("reallocarray");
950 n = fread(buf + total, 1, remain, f);
953 err = got_ferror(f, GOT_ERR_IO);