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>
21 #include <sys/socket.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);
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 (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
126 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
127 SIMPLEQ_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 (!SIMPLEQ_EMPTY(ids)) {
147 qid = SIMPLEQ_FIRST(ids);
148 SIMPLEQ_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 SIMPLEQ_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 SIMPLEQ_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);
345 /* Timestamp is separated from committer name + email by space. */
346 space = strrchr(committer, ' ');
348 return got_error(GOT_ERR_BAD_OBJ_DATA);
350 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
351 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
353 return got_error(GOT_ERR_BAD_OBJ_DATA);
355 /* Strip off parsed time information, leaving just author and email. */
362 got_object_commit_close(struct got_commit_object *commit)
364 if (commit->refcnt > 0) {
366 if (commit->refcnt > 0)
370 got_object_id_queue_free(&commit->parent_ids);
371 free(commit->tree_id);
372 free(commit->author);
373 free(commit->committer);
374 free(commit->logmsg);
378 struct got_object_id *
379 got_object_commit_get_tree_id(struct got_commit_object *commit)
381 return commit->tree_id;
385 got_object_commit_get_nparents(struct got_commit_object *commit)
387 return commit->nparents;
390 const struct got_object_id_queue *
391 got_object_commit_get_parent_ids(struct got_commit_object *commit)
393 return &commit->parent_ids;
397 got_object_commit_get_author(struct got_commit_object *commit)
399 return commit->author;
403 got_object_commit_get_author_time(struct got_commit_object *commit)
405 return commit->author_time;
408 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
410 return commit->author_gmtoff;
414 got_object_commit_get_committer(struct got_commit_object *commit)
416 return commit->committer;
420 got_object_commit_get_committer_time(struct got_commit_object *commit)
422 return commit->committer_time;
426 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
428 return commit->committer_gmtoff;
431 const struct got_error *
432 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
434 const struct got_error *err = NULL;
435 char *msg0, *msg, *line, *s;
441 msg0 = strdup(commit->logmsg);
443 return got_error_from_errno("strdup");
445 /* Copy log message line by line to strip out unusual headers... */
448 if ((line = strsep(&msg, "\n")) == NULL)
452 if (line[0] != '\0' &&
453 strncmp(line, GOT_COMMIT_LABEL_TREE,
454 strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
455 strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
456 strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
457 strncmp(line, GOT_COMMIT_LABEL_PARENT,
458 strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
459 strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
460 strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
467 if (asprintf(&s, "%s%s\n",
468 *logmsg ? *logmsg : "", line) == -1) {
469 err = got_error_from_errno("asprintf");
477 if (*logmsg == NULL) {
478 /* log message does not contain \n */
479 *logmsg = strdup(commit->logmsg);
480 if (*logmsg == NULL) {
481 err = got_error_from_errno("strdup");
486 /* Trim redundant trailing whitespace. */
487 len = strlen(*logmsg);
488 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
489 isspace((unsigned char)(*logmsg)[len - 1])) {
490 (*logmsg)[len - 1] = '\0';
503 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
505 return commit->logmsg;
508 const struct got_error *
509 got_object_parse_commit(struct got_commit_object **commit, char *buf,
512 const struct got_error *err = NULL;
515 ssize_t remain = (ssize_t)len;
518 return got_error(GOT_ERR_BAD_OBJ_DATA);
520 *commit = got_object_commit_alloc_partial();
522 return got_error_from_errno("got_object_commit_alloc_partial");
524 label_len = strlen(GOT_COMMIT_LABEL_TREE);
525 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
527 if (remain < SHA1_DIGEST_STRING_LENGTH) {
528 err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
533 err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 remain -= SHA1_DIGEST_STRING_LENGTH;
537 s += SHA1_DIGEST_STRING_LENGTH;
539 err = got_error(GOT_ERR_BAD_OBJ_DATA);
543 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
544 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
546 if (remain < SHA1_DIGEST_STRING_LENGTH) {
547 err = got_error(GOT_ERR_BAD_OBJ_DATA);
551 err = got_object_commit_add_parent(*commit, s);
555 remain -= SHA1_DIGEST_STRING_LENGTH;
556 s += SHA1_DIGEST_STRING_LENGTH;
559 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
560 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
566 err = got_error(GOT_ERR_BAD_OBJ_DATA);
570 p = memchr(s, '\n', remain);
572 err = got_error(GOT_ERR_BAD_OBJ_DATA);
577 err = parse_commit_time(&(*commit)->author_time,
578 &(*commit)->author_gmtoff, s);
581 (*commit)->author = strdup(s);
582 if ((*commit)->author == NULL) {
583 err = got_error_from_errno("strdup");
590 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
591 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
597 err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 p = memchr(s, '\n', remain);
603 err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 err = parse_commit_time(&(*commit)->committer_time,
609 &(*commit)->committer_gmtoff, s);
612 (*commit)->committer = strdup(s);
613 if ((*commit)->committer == NULL) {
614 err = got_error_from_errno("strdup");
621 (*commit)->logmsg = strndup(s, remain);
622 if ((*commit)->logmsg == NULL) {
623 err = got_error_from_errno("strndup");
628 got_object_commit_close(*commit);
635 got_object_tree_close(struct got_tree_object *tree)
637 if (tree->refcnt > 0) {
639 if (tree->refcnt > 0)
647 static const struct got_error *
648 parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
649 size_t *elen, char *buf,
653 const struct got_error *err = NULL;
658 *pte = malloc(sizeof(**pte));
660 return got_error_from_errno("malloc");
662 *elen = strnlen(buf, maxlen) + 1;
663 if (*elen > maxlen) {
666 return got_error(GOT_ERR_BAD_OBJ_DATA);
669 space = memchr(buf, ' ', *elen);
670 if (space == NULL || space <= buf) {
671 err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 if (*p < '0' && *p > '7') {
680 err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 (*pte)->mode |= *p - '0';
688 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
689 err = got_error(GOT_ERR_BAD_OBJ_DATA);
695 *elen += SHA1_DIGEST_LENGTH;
704 const struct got_error *
705 got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
706 uint8_t *buf, size_t len)
708 const struct got_error *err = NULL;
713 return NULL; /* tree is empty */
716 struct got_parsed_tree_entry *pte;
717 struct got_pathlist_entry *new = NULL;
721 err = parse_tree_entry(&pte, &name, &elen, buf, remain);
724 err = got_pathlist_insert(&new, entries, name, pte);
728 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
737 err = got_error(GOT_ERR_BAD_OBJ_DATA);
742 got_object_parsed_tree_entries_free(entries);
749 got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
751 struct got_pathlist_entry *pe;
753 TAILQ_FOREACH(pe, entries, entry) {
754 struct got_parsed_tree_entry *pte = pe->data;
757 got_pathlist_free(entries);
761 got_object_tag_close(struct got_tag_object *tag)
763 if (tag->refcnt > 0) {
775 const struct got_error *
776 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
778 const struct got_error *err = NULL;
784 return got_error(GOT_ERR_BAD_OBJ_DATA);
786 *tag = calloc(1, sizeof(**tag));
788 return got_error_from_errno("calloc");
790 label_len = strlen(GOT_TAG_LABEL_OBJECT);
791 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
793 if (remain < SHA1_DIGEST_STRING_LENGTH) {
794 err = got_error(GOT_ERR_BAD_OBJ_DATA);
798 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
799 err = got_error(GOT_ERR_BAD_OBJ_DATA);
802 remain -= SHA1_DIGEST_STRING_LENGTH;
803 s += SHA1_DIGEST_STRING_LENGTH;
805 err = got_error(GOT_ERR_BAD_OBJ_DATA);
810 err = got_error(GOT_ERR_BAD_OBJ_DATA);
814 label_len = strlen(GOT_TAG_LABEL_TYPE);
815 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
818 err = got_error(GOT_ERR_BAD_OBJ_DATA);
822 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
823 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
824 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
825 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
828 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
829 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
830 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
831 label_len = strlen(GOT_OBJ_LABEL_TREE);
834 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
835 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
836 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
837 label_len = strlen(GOT_OBJ_LABEL_BLOB);
840 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
841 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
842 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
843 label_len = strlen(GOT_OBJ_LABEL_TAG);
847 err = got_error(GOT_ERR_BAD_OBJ_DATA);
851 if (remain <= 0 || *s != '\n') {
852 err = got_error(GOT_ERR_BAD_OBJ_DATA);
858 err = got_error(GOT_ERR_BAD_OBJ_DATA);
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 label_len = strlen(GOT_TAG_LABEL_TAG);
867 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
872 err = got_error(GOT_ERR_BAD_OBJ_DATA);
876 p = memchr(s, '\n', remain);
878 err = got_error(GOT_ERR_BAD_OBJ_DATA);
883 (*tag)->tag = strndup(s, slen);
884 if ((*tag)->tag == NULL) {
885 err = got_error_from_errno("strndup");
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_TAGGER);
900 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
910 p = memchr(s, '\n', remain);
912 err = got_error(GOT_ERR_BAD_OBJ_DATA);
917 err = parse_commit_time(&(*tag)->tagger_time,
918 &(*tag)->tagger_gmtoff, s);
921 (*tag)->tagger = strdup(s);
922 if ((*tag)->tagger == NULL) {
923 err = got_error_from_errno("strdup");
929 err = got_error(GOT_ERR_BAD_OBJ_DATA);
933 /* Some old tags in the Linux git repo have no tagger. */
934 (*tag)->tagger = strdup("");
935 if ((*tag)->tagger == NULL) {
936 err = got_error_from_errno("strdup");
941 (*tag)->tagmsg = strndup(s, remain);
942 if ((*tag)->tagmsg == NULL) {
943 err = got_error_from_errno("strndup");
948 got_object_tag_close(*tag);
954 const struct got_error *
955 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
957 const struct got_error *err = NULL;
958 static const size_t blocksize = 512;
959 size_t n, total, remain;
965 buf = malloc(blocksize);
967 return got_error_from_errno("malloc");
974 newbuf = reallocarray(buf, 1, total + blocksize);
975 if (newbuf == NULL) {
976 err = got_error_from_errno("reallocarray");
982 n = fread(buf + total, 1, remain, f);
985 err = got_ferror(f, GOT_ERR_IO);