2 * Copyright (c) 2018 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"
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_cache.h"
48 #include "got_lib_pack.h"
49 #include "got_lib_privsep.h"
50 #include "got_lib_repository.h"
51 #include "got_lib_path.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();
73 (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 if ((*qid)->id == NULL) {
75 err = got_error_from_errno();
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();
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 got_delta_close(delta);
119 if (obj->flags & GOT_OBJ_FLAG_PACKED)
120 free(obj->path_packfile);
125 got_object_qid_free(struct got_object_qid *qid)
131 const struct got_error *
132 got_object_parse_header(struct got_object **obj, char *buf, size_t len)
134 const char *obj_labels[] = {
135 GOT_OBJ_LABEL_COMMIT,
140 const int obj_types[] = {
147 size_t size = 0, hdrlen = 0;
149 char *p = strchr(buf, '\0');
154 return got_error(GOT_ERR_BAD_OBJ_HDR);
156 hdrlen = strlen(buf) + 1 /* '\0' */;
158 for (i = 0; i < nitems(obj_labels); i++) {
159 const char *label = obj_labels[i];
160 size_t label_len = strlen(label);
163 if (strncmp(buf, label, label_len) != 0)
167 if (len <= label_len)
168 return got_error(GOT_ERR_BAD_OBJ_HDR);
169 size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
171 return got_error(GOT_ERR_BAD_OBJ_HDR);
176 return got_error(GOT_ERR_BAD_OBJ_HDR);
178 *obj = calloc(1, sizeof(**obj));
180 return got_error_from_errno();
182 (*obj)->hdrlen = hdrlen;
187 const struct got_error *
188 got_object_read_header(struct got_object **obj, int fd)
190 const struct got_error *err;
191 struct got_inflate_buf zb;
193 const size_t zbsize = 64;
194 size_t outlen, totlen;
199 buf = malloc(zbsize);
201 return got_error_from_errno();
203 err = got_inflate_init(&zb, buf, zbsize);
209 err = got_inflate_read_fd(&zb, fd, &outlen);
215 if (strchr(zb.outbuf, '\0') == NULL) {
218 newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
219 if (newbuf == NULL) {
220 err = got_error_from_errno();
224 zb.outbuf = newbuf + totlen;
225 zb.outlen = (nbuf * zbsize) - totlen;
227 } while (strchr(zb.outbuf, '\0') == NULL);
229 err = got_object_parse_header(obj, buf, totlen);
232 got_inflate_end(&zb);
236 struct got_commit_object *
237 got_object_commit_alloc_partial(void)
239 struct got_commit_object *commit;
241 commit = calloc(1, sizeof(*commit));
244 commit->tree_id = malloc(sizeof(*commit->tree_id));
245 if (commit->tree_id == NULL) {
250 SIMPLEQ_INIT(&commit->parent_ids);
255 const struct got_error *
256 got_object_commit_add_parent(struct got_commit_object *commit,
259 const struct got_error *err = NULL;
260 struct got_object_qid *qid;
262 err = got_object_qid_alloc_partial(&qid);
266 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
267 err = got_error(GOT_ERR_BAD_OBJ_DATA);
273 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
279 static const struct got_error *
280 parse_gmtoff(time_t *gmtoff, const char *tzstr)
283 const char *p = tzstr;
291 return got_error(GOT_ERR_BAD_OBJ_DATA);
293 if (!isdigit(*p) && !isdigit(*(p + 1)))
294 return got_error(GOT_ERR_BAD_OBJ_DATA);
295 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
298 if (!isdigit(*p) && !isdigit(*(p + 1)))
299 return got_error(GOT_ERR_BAD_OBJ_DATA);
300 m = ((*p - '0') * 10) + (*(p + 1) - '0');
302 *gmtoff = (h * 60 * 60 + m * 60) * sign;
306 static const struct got_error *
307 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
309 const struct got_error *err = NULL;
313 /* Parse and strip off trailing timezone indicator string. */
314 space = strrchr(committer, ' ');
316 return got_error(GOT_ERR_BAD_OBJ_DATA);
317 tzstr = strdup(space + 1);
319 return got_error_from_errno();
320 err = parse_gmtoff(gmtoff, tzstr);
326 /* Timestamp is separated from committer name + email by space. */
327 space = strrchr(committer, ' ');
329 return got_error(GOT_ERR_BAD_OBJ_DATA);
331 /* Timestamp parsed here is expressed in comitter's local time. */
332 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
334 return got_error(GOT_ERR_BAD_OBJ_DATA);
336 /* Express the time stamp in UTC. */
339 /* Strip off parsed time information, leaving just author and email. */
346 got_object_commit_close(struct got_commit_object *commit)
348 struct got_object_qid *qid;
350 if (commit->refcnt > 0) {
352 if (commit->refcnt > 0)
356 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
357 qid = SIMPLEQ_FIRST(&commit->parent_ids);
358 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
359 got_object_qid_free(qid);
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;
423 got_object_commit_get_logmsg(struct got_commit_object *commit)
425 return commit->logmsg;
428 const struct got_error *
429 got_object_parse_commit(struct got_commit_object **commit, char *buf,
432 const struct got_error *err = NULL;
435 ssize_t remain = (ssize_t)len;
437 *commit = got_object_commit_alloc_partial();
439 return got_error_from_errno();
441 label_len = strlen(GOT_COMMIT_LABEL_TREE);
442 if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
444 if (remain < SHA1_DIGEST_STRING_LENGTH) {
445 err = got_error(GOT_ERR_BAD_OBJ_DATA);
449 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
450 err = got_error(GOT_ERR_BAD_OBJ_DATA);
453 remain -= SHA1_DIGEST_STRING_LENGTH;
454 s += SHA1_DIGEST_STRING_LENGTH;
456 err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 label_len = strlen(GOT_COMMIT_LABEL_PARENT);
461 while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
463 if (remain < SHA1_DIGEST_STRING_LENGTH) {
464 err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 err = got_object_commit_add_parent(*commit, s);
472 remain -= SHA1_DIGEST_STRING_LENGTH;
473 s += SHA1_DIGEST_STRING_LENGTH;
476 label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
477 if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
483 err = got_error(GOT_ERR_BAD_OBJ_DATA);
489 err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 err = parse_commit_time(&(*commit)->author_time,
495 &(*commit)->author_gmtoff, s);
498 (*commit)->author = strdup(s);
499 if ((*commit)->author == NULL) {
500 err = got_error_from_errno();
507 label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
508 if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
514 err = got_error(GOT_ERR_BAD_OBJ_DATA);
520 err = got_error(GOT_ERR_BAD_OBJ_DATA);
525 err = parse_commit_time(&(*commit)->committer_time,
526 &(*commit)->committer_gmtoff, s);
529 (*commit)->committer = strdup(s);
530 if ((*commit)->committer == NULL) {
531 err = got_error_from_errno();
538 (*commit)->logmsg = strndup(s, remain);
539 if ((*commit)->logmsg == NULL) {
540 err = got_error_from_errno();
545 got_object_commit_close(*commit);
552 got_object_tree_entry_close(struct got_tree_entry *te)
560 got_object_tree_close(struct got_tree_object *tree)
562 struct got_tree_entry *te;
564 if (tree->refcnt > 0) {
566 if (tree->refcnt > 0)
570 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
571 te = SIMPLEQ_FIRST(&tree->entries.head);
572 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
573 got_object_tree_entry_close(te);
579 struct got_tree_entry *
580 got_alloc_tree_entry_partial(void)
582 struct got_tree_entry *te;
584 te = malloc(sizeof(*te));
588 te->id = malloc(sizeof(*te->id));
589 if (te->id == NULL) {
596 static const struct got_error *
597 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
600 char *p = buf, *space;
601 const struct got_error *err = NULL;
603 *te = got_alloc_tree_entry_partial();
605 return got_error_from_errno();
607 *elen = strlen(buf) + 1;
608 if (*elen > maxlen) {
611 return got_error(GOT_ERR_BAD_OBJ_DATA);
614 space = strchr(buf, ' ');
616 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 if (*p < '0' && *p > '7') {
624 err = got_error(GOT_ERR_BAD_OBJ_DATA);
628 (*te)->mode |= *p - '0';
632 (*te)->name = strdup(space + 1);
633 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
634 err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
639 *elen += SHA1_DIGEST_LENGTH;
642 got_object_tree_entry_close(*te);
648 const struct got_error *
649 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
651 const struct got_error *err;
653 struct got_pathlist_head pathlist;
654 struct got_pathlist_entry *pe;
656 TAILQ_INIT(&pathlist);
658 *tree = calloc(1, sizeof(**tree));
660 return got_error_from_errno();
662 SIMPLEQ_INIT(&(*tree)->entries.head);
665 struct got_tree_entry *te;
666 struct got_pathlist_entry *new = NULL;
669 err = parse_tree_entry(&te, &elen, buf, remain);
672 err = got_pathlist_insert(&new, &pathlist, te->name, te);
676 err = got_error(GOT_ERR_TREE_DUP_ENTRY);
684 got_object_tree_close(*tree);
686 err = got_error(GOT_ERR_BAD_OBJ_DATA);
690 TAILQ_FOREACH(pe, &pathlist, entry) {
691 struct got_tree_entry *te = pe->data;
692 (*tree)->entries.nentries++;
693 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
696 got_pathlist_free(&pathlist);
701 got_object_tag_close(struct got_tag_object *tag)
709 const struct got_error *
710 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
712 const struct got_error *err = NULL;
717 *tag = calloc(1, sizeof(**tag));
719 return got_error_from_errno();
721 label_len = strlen(GOT_TAG_LABEL_OBJECT);
722 if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
724 if (remain < SHA1_DIGEST_STRING_LENGTH) {
725 err = got_error(GOT_ERR_BAD_OBJ_DATA);
729 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
730 err = got_error(GOT_ERR_BAD_OBJ_DATA);
733 remain -= SHA1_DIGEST_STRING_LENGTH;
734 s += SHA1_DIGEST_STRING_LENGTH;
736 err = got_error(GOT_ERR_BAD_OBJ_DATA);
741 err = got_error(GOT_ERR_BAD_OBJ_DATA);
745 label_len = strlen(GOT_TAG_LABEL_TYPE);
746 if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
749 err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
754 strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
755 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
756 label_len = strlen(GOT_OBJ_LABEL_COMMIT);
759 } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
760 strlen(GOT_OBJ_LABEL_TREE)) == 0) {
761 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
762 label_len = strlen(GOT_OBJ_LABEL_TREE);
765 } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
766 strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
767 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
768 label_len = strlen(GOT_OBJ_LABEL_BLOB);
771 } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
772 strlen(GOT_OBJ_LABEL_TAG)) == 0) {
773 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
774 label_len = strlen(GOT_OBJ_LABEL_TAG);
778 err = got_error(GOT_ERR_BAD_OBJ_DATA);
782 if (remain <= 0 || *s != '\n') {
783 err = got_error(GOT_ERR_BAD_OBJ_DATA);
789 err = got_error(GOT_ERR_BAD_OBJ_DATA);
793 err = got_error(GOT_ERR_BAD_OBJ_DATA);
797 label_len = strlen(GOT_TAG_LABEL_TAG);
798 if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
803 err = got_error(GOT_ERR_BAD_OBJ_DATA);
809 err = got_error(GOT_ERR_BAD_OBJ_DATA);
814 (*tag)->tag = strndup(s, slen);
815 if ((*tag)->tag == NULL) {
816 err = got_error_from_errno();
822 err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 err = got_error(GOT_ERR_BAD_OBJ_DATA);
830 label_len = strlen(GOT_TAG_LABEL_TAGGER);
831 if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
837 err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 err = parse_commit_time(&(*tag)->tagger_time,
849 &(*tag)->tagger_gmtoff, s);
852 (*tag)->tagger = strdup(s);
853 if ((*tag)->tagger == NULL) {
854 err = got_error_from_errno();
860 err = got_error(GOT_ERR_BAD_OBJ_DATA);
864 /* Some old tags in the Linux git repo have no tagger. */
865 (*tag)->tagger = strdup("");
866 if ((*tag)->tagger == NULL) {
867 err = got_error_from_errno();
872 (*tag)->tagmsg = strndup(s, remain);
873 if ((*tag)->tagmsg == NULL) {
874 err = got_error_from_errno();
879 got_object_tag_close(*tag);
885 const struct got_error *
886 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
888 const struct got_error *err = NULL;
889 static const size_t blocksize = 512;
890 size_t n, total, remain;
896 buf = malloc(blocksize);
898 return got_error_from_errno();
905 newbuf = reallocarray(buf, 1, total + blocksize);
906 if (newbuf == NULL) {
907 err = got_error_from_errno();
913 n = fread(buf + total, 1, remain, f);
916 err = got_ferror(f, GOT_ERR_IO);