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_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]))
58 got_object_id_cmp(const struct got_object_id *id1,
59 const struct got_object_id *id2)
61 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
64 const struct got_error *
65 got_object_qid_alloc_partial(struct got_object_qid **qid)
67 const struct got_error *err = NULL;
69 *qid = malloc(sizeof(**qid));
71 return got_error_from_errno("malloc");
73 (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 if ((*qid)->id == NULL) {
75 err = got_error_from_errno("malloc");
76 got_object_qid_free(*qid);
84 const struct got_error *
85 got_object_id_str(char **outbuf, struct got_object_id *id)
87 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
89 *outbuf = malloc(len);
91 return got_error_from_errno("malloc");
93 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
96 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
103 got_object_close(struct got_object *obj)
105 if (obj->refcnt > 0) {
111 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
112 struct got_delta *delta;
113 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
114 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
115 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
116 free(delta->delta_buf);
120 if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 free(obj->path_packfile);
126 got_object_qid_free(struct got_object_qid *qid)
133 got_object_id_queue_free(struct got_object_id_queue *ids)
135 struct got_object_qid *qid;
137 while (!SIMPLEQ_EMPTY(ids)) {
138 qid = SIMPLEQ_FIRST(ids);
139 SIMPLEQ_REMOVE_HEAD(ids, entry);
140 got_object_qid_free(qid);
144 const struct got_error *
145 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
147 const char *obj_labels[] = {
148 GOT_OBJ_LABEL_COMMIT,
153 const int obj_types[] = {
160 size_t size = 0, hdrlen = 0;
165 hdrlen = strnlen(buf, len) + 1 /* '\0' */;
167 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 for (i = 0; i < nitems(obj_labels); i++) {
170 const char *label = obj_labels[i];
171 size_t label_len = strlen(label);
174 if (strncmp(buf, label, label_len) != 0)
178 if (len <= label_len)
179 return got_error(GOT_ERR_BAD_OBJ_HDR);
180 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
182 return got_error(GOT_ERR_BAD_OBJ_HDR);
187 return got_error(GOT_ERR_BAD_OBJ_HDR);
189 *obj = calloc(1, sizeof(**obj));
191 return got_error_from_errno("calloc");
193 (*obj)->hdrlen = hdrlen;
198 const struct got_error *
199 got_object_read_header(struct got_object **obj, int fd)
201 const struct got_error *err;
202 struct got_inflate_buf zb;
204 const size_t zbsize = 64;
205 size_t outlen, totlen;
210 buf = malloc(zbsize);
212 return got_error_from_errno("malloc");
214 err = got_inflate_init(&zb, buf, zbsize);
220 err = got_inflate_read_fd(&zb, fd, &outlen);
226 if (memchr(zb.outbuf, '\0', outlen) == NULL) {
229 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
230 if (newbuf == NULL) {
231 err = got_error_from_errno("recallocarray");
235 zb.outbuf = newbuf + totlen;
236 zb.outlen = (nbuf * zbsize) - totlen;
238 } while (memchr(zb.outbuf, '\0', outlen) == NULL);
240 err = got_object_parse_header(obj, buf, totlen);
243 got_inflate_end(&zb);
247 struct got_commit_object *
248 got_object_commit_alloc_partial(void)
250 struct got_commit_object *commit;
252 commit = calloc(1, sizeof(*commit));
255 commit->tree_id = malloc(sizeof(*commit->tree_id));
256 if (commit->tree_id == NULL) {
261 SIMPLEQ_INIT(&commit->parent_ids);
266 const struct got_error *
267 got_object_commit_add_parent(struct got_commit_object *commit,
270 const struct got_error *err = NULL;
271 struct got_object_qid *qid;
273 err = got_object_qid_alloc_partial(&qid);
277 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
278 err = got_error(GOT_ERR_BAD_OBJ_DATA);
279 got_object_qid_free(qid);
283 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
289 static const struct got_error *
290 parse_gmtoff(time_t *gmtoff, const char *tzstr)
293 const char *p = tzstr;
301 return got_error(GOT_ERR_BAD_OBJ_DATA);
303 if (!isdigit(*p) && !isdigit(*(p + 1)))
304 return got_error(GOT_ERR_BAD_OBJ_DATA);
305 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
308 if (!isdigit(*p) && !isdigit(*(p + 1)))
309 return got_error(GOT_ERR_BAD_OBJ_DATA);
310 m = ((*p - '0') * 10) + (*(p + 1) - '0');
312 *gmtoff = (h * 60 * 60 + m * 60) * sign;
316 static const struct got_error *
317 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
319 const struct got_error *err = NULL;
323 /* Parse and strip off trailing timezone indicator string. */
324 space = strrchr(committer, ' ');
326 return got_error(GOT_ERR_BAD_OBJ_DATA);
327 tzstr = strdup(space + 1);
329 return got_error_from_errno("strdup");
330 err = parse_gmtoff(gmtoff, tzstr);
336 /* Timestamp is separated from committer name + email by space. */
337 space = strrchr(committer, ' ');
339 return got_error(GOT_ERR_BAD_OBJ_DATA);
341 /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
342 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
344 return got_error(GOT_ERR_BAD_OBJ_DATA);
346 /* Strip off parsed time information, leaving just author and email. */
353 got_object_commit_close(struct got_commit_object *commit)
355 if (commit->refcnt > 0) {
357 if (commit->refcnt > 0)
361 got_object_id_queue_free(&commit->parent_ids);
362 free(commit->tree_id);
363 free(commit->author);
364 free(commit->committer);
365 free(commit->logmsg);
369 struct got_object_id *
370 got_object_commit_get_tree_id(struct got_commit_object *commit)
372 return commit->tree_id;
376 got_object_commit_get_nparents(struct got_commit_object *commit)
378 return commit->nparents;
381 const struct got_object_id_queue *
382 got_object_commit_get_parent_ids(struct got_commit_object *commit)
384 return &commit->parent_ids;
388 got_object_commit_get_author(struct got_commit_object *commit)
390 return commit->author;
394 got_object_commit_get_author_time(struct got_commit_object *commit)
396 return commit->author_time;
399 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
401 return commit->author_gmtoff;
405 got_object_commit_get_committer(struct got_commit_object *commit)
407 return commit->committer;
411 got_object_commit_get_committer_time(struct got_commit_object *commit)
413 return commit->committer_time;
417 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
419 return commit->committer_gmtoff;
422 #define GOT_GPG_BEGIN_STR "gpgsig -----BEGIN PGP SIGNATURE-----"
423 #define GOT_GPG_END_STR " -----END PGP SIGNATURE-----"
425 const struct got_error *
426 got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
428 const struct got_error *err = NULL;
430 char *msg0, *msg, *line, *s;
435 msg0 = strdup(commit->logmsg);
437 return got_error_from_errno("strdup");
439 /* Copy log message line by line to strip out GPG sigs... */
442 line = strsep(&msg, "\n");
445 /* Skip over GPG signatures. */
447 if (strcmp(line, GOT_GPG_END_STR) == 0) {
449 /* Skip empty line after sig. */
450 line = strsep(&msg, "\n");
453 } else if (strcmp(line, GOT_GPG_BEGIN_STR) == 0) {
457 if (asprintf(&s, "%s%s\n",
458 *logmsg ? *logmsg : "", line) == -1) {
459 err = got_error_from_errno("asprintf");
467 /* Trim redundant trailing whitespace. */
468 len = strlen(*logmsg);
469 while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
470 isspace((unsigned char)(*logmsg)[len - 1])) {
471 (*logmsg)[len - 1] = '\0';
484 got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
486 return commit->logmsg;
489 const struct got_error *
490 got_object_parse_commit(struct got_commit_object **commit, char *buf,
493 const struct got_error *err = NULL;
496 ssize_t remain = (ssize_t)len;
499 return got_error(GOT_ERR_BAD_OBJ_DATA);
501 *commit = got_object_commit_alloc_partial();
503 return got_error_from_errno("got_object_commit_alloc_partial");
505 label_len = strlen(GOT_COMMIT_LABEL_TREE);
506 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
508 if (remain < SHA1_DIGEST_STRING_LENGTH) {
509 err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
514 err = got_error(GOT_ERR_BAD_OBJ_DATA);
517 remain -= SHA1_DIGEST_STRING_LENGTH;
518 s += SHA1_DIGEST_STRING_LENGTH;
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
524 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
525 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
527 if (remain < SHA1_DIGEST_STRING_LENGTH) {
528 err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 err = got_object_commit_add_parent(*commit, s);
536 remain -= SHA1_DIGEST_STRING_LENGTH;
537 s += SHA1_DIGEST_STRING_LENGTH;
540 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
541 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
547 err = got_error(GOT_ERR_BAD_OBJ_DATA);
551 p = memchr(s, '\n', remain);
553 err = got_error(GOT_ERR_BAD_OBJ_DATA);
558 err = parse_commit_time(&(*commit)->author_time,
559 &(*commit)->author_gmtoff, s);
562 (*commit)->author = strdup(s);
563 if ((*commit)->author == NULL) {
564 err = got_error_from_errno("strdup");
571 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
572 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
578 err = got_error(GOT_ERR_BAD_OBJ_DATA);
582 p = memchr(s, '\n', remain);
584 err = got_error(GOT_ERR_BAD_OBJ_DATA);
589 err = parse_commit_time(&(*commit)->committer_time,
590 &(*commit)->committer_gmtoff, s);
593 (*commit)->committer = strdup(s);
594 if ((*commit)->committer == NULL) {
595 err = got_error_from_errno("strdup");
602 (*commit)->logmsg = strndup(s, remain);
603 if ((*commit)->logmsg == NULL) {
604 err = got_error_from_errno("strndup");
609 got_object_commit_close(*commit);
616 got_object_tree_entry_close(struct got_tree_entry *te)
624 got_object_tree_entries_close(struct got_tree_entries *entries)
626 struct got_tree_entry *te;
628 while (!SIMPLEQ_EMPTY(&entries->head)) {
629 te = SIMPLEQ_FIRST(&entries->head);
630 SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
631 got_object_tree_entry_close(te);
636 got_object_tree_close(struct got_tree_object *tree)
638 if (tree->refcnt > 0) {
640 if (tree->refcnt > 0)
644 got_object_tree_entries_close(&tree->entries);
648 struct got_tree_entry *
649 got_alloc_tree_entry_partial(void)
651 struct got_tree_entry *te;
653 te = malloc(sizeof(*te));
657 te->id = malloc(sizeof(*te->id));
658 if (te->id == NULL) {
665 static const struct got_error *
666 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
670 const struct got_error *err = NULL;
674 *te = got_alloc_tree_entry_partial();
676 return got_error_from_errno("got_alloc_tree_entry_partial");
678 *elen = strnlen(buf, maxlen) + 1;
679 if (*elen > maxlen) {
682 return got_error(GOT_ERR_BAD_OBJ_DATA);
685 space = memchr(buf, ' ', *elen);
686 if (space == NULL || space <= buf) {
687 err = got_error(GOT_ERR_BAD_OBJ_DATA);
695 if (*p < '0' && *p > '7') {
696 err = got_error(GOT_ERR_BAD_OBJ_DATA);
700 (*te)->mode |= *p - '0';
704 (*te)->name = strdup(space + 1);
705 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
706 err = got_error(GOT_ERR_BAD_OBJ_DATA);
710 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
711 *elen += SHA1_DIGEST_LENGTH;
714 got_object_tree_entry_close(*te);
720 const struct got_error *
721 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
723 const struct got_error *err;
725 struct got_pathlist_head pathlist;
726 struct got_pathlist_entry *pe;
728 TAILQ_INIT(&pathlist);
731 return got_error(GOT_ERR_BAD_OBJ_DATA);
733 *tree = calloc(1, sizeof(**tree));
735 return got_error_from_errno("calloc");
737 SIMPLEQ_INIT(&(*tree)->entries.head);
740 struct got_tree_entry *te;
741 struct got_pathlist_entry *new = NULL;
744 err = parse_tree_entry(&te, &elen, buf, remain);
747 err = got_pathlist_insert(&new, &pathlist, te->name, te);
751 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
759 got_object_tree_close(*tree);
761 err = got_error(GOT_ERR_BAD_OBJ_DATA);
765 TAILQ_FOREACH(pe, &pathlist, entry) {
766 struct got_tree_entry *te = pe->data;
767 (*tree)->entries.nentries++;
768 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
771 got_pathlist_free(&pathlist);
776 got_object_tag_close(struct got_tag_object *tag)
778 if (tag->refcnt > 0) {
790 const struct got_error *
791 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
793 const struct got_error *err = NULL;
799 return got_error(GOT_ERR_BAD_OBJ_DATA);
801 *tag = calloc(1, sizeof(**tag));
803 return got_error_from_errno("calloc");
805 label_len = strlen(GOT_TAG_LABEL_OBJECT);
806 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
808 if (remain < SHA1_DIGEST_STRING_LENGTH) {
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
813 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
814 err = got_error(GOT_ERR_BAD_OBJ_DATA);
817 remain -= SHA1_DIGEST_STRING_LENGTH;
818 s += SHA1_DIGEST_STRING_LENGTH;
820 err = got_error(GOT_ERR_BAD_OBJ_DATA);
825 err = got_error(GOT_ERR_BAD_OBJ_DATA);
829 label_len = strlen(GOT_TAG_LABEL_TYPE);
830 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
833 err = got_error(GOT_ERR_BAD_OBJ_DATA);
837 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
838 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
839 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
840 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
843 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
844 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
845 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
846 label_len = strlen(GOT_OBJ_LABEL_TREE);
849 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
850 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
851 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
852 label_len = strlen(GOT_OBJ_LABEL_BLOB);
855 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
856 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
857 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
858 label_len = strlen(GOT_OBJ_LABEL_TAG);
862 err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 if (remain <= 0 || *s != '\n') {
867 err = got_error(GOT_ERR_BAD_OBJ_DATA);
873 err = got_error(GOT_ERR_BAD_OBJ_DATA);
877 err = got_error(GOT_ERR_BAD_OBJ_DATA);
881 label_len = strlen(GOT_TAG_LABEL_TAG);
882 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
887 err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 p = memchr(s, '\n', remain);
893 err = got_error(GOT_ERR_BAD_OBJ_DATA);
898 (*tag)->tag = strndup(s, slen);
899 if ((*tag)->tag == NULL) {
900 err = got_error_from_errno("strndup");
906 err = got_error(GOT_ERR_BAD_OBJ_DATA);
910 err = got_error(GOT_ERR_BAD_OBJ_DATA);
914 label_len = strlen(GOT_TAG_LABEL_TAGGER);
915 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
921 err = got_error(GOT_ERR_BAD_OBJ_DATA);
925 p = memchr(s, '\n', remain);
927 err = got_error(GOT_ERR_BAD_OBJ_DATA);
932 err = parse_commit_time(&(*tag)->tagger_time,
933 &(*tag)->tagger_gmtoff, s);
936 (*tag)->tagger = strdup(s);
937 if ((*tag)->tagger == NULL) {
938 err = got_error_from_errno("strdup");
944 err = got_error(GOT_ERR_BAD_OBJ_DATA);
948 /* Some old tags in the Linux git repo have no tagger. */
949 (*tag)->tagger = strdup("");
950 if ((*tag)->tagger == NULL) {
951 err = got_error_from_errno("strdup");
956 (*tag)->tagmsg = strndup(s, remain);
957 if ((*tag)->tagmsg == NULL) {
958 err = got_error_from_errno("strndup");
963 got_object_tag_close(*tag);
969 const struct got_error *
970 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
972 const struct got_error *err = NULL;
973 static const size_t blocksize = 512;
974 size_t n, total, remain;
980 buf = malloc(blocksize);
982 return got_error_from_errno("malloc");
989 newbuf = reallocarray(buf, 1, total + blocksize);
990 if (newbuf == NULL) {
991 err = got_error_from_errno("reallocarray");
997 n = fread(buf + total, 1, remain, f);
1000 err = got_ferror(f, GOT_ERR_IO);