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"
53 #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 #define GOT_OBJ_TAG_COMMIT "commit"
57 #define GOT_OBJ_TAG_TREE "tree"
58 #define GOT_OBJ_TAG_BLOB "blob"
59 #define GOT_OBJ_TAG_TAG "tag"
61 #define GOT_COMMIT_TAG_TREE "tree "
62 #define GOT_COMMIT_TAG_PARENT "parent "
63 #define GOT_COMMIT_TAG_AUTHOR "author "
64 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 #define GOT_TAG_TAG_OBJECT "object "
67 #define GOT_TAG_TAG_TYPE "type "
68 #define GOT_TAG_TAG_TAG "tag "
69 #define GOT_TAG_TAG_TAGGER "tagger "
72 got_object_id_cmp(const struct got_object_id *id1,
73 const struct got_object_id *id2)
75 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
78 const struct got_error *
79 got_object_qid_alloc_partial(struct got_object_qid **qid)
81 const struct got_error *err = NULL;
83 *qid = malloc(sizeof(**qid));
85 return got_error_from_errno();
87 (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 if ((*qid)->id == NULL) {
89 err = got_error_from_errno();
90 got_object_qid_free(*qid);
98 const struct got_error *
99 got_object_id_str(char **outbuf, struct got_object_id *id)
101 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
103 *outbuf = malloc(len);
105 return got_error_from_errno();
107 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
110 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
117 got_object_close(struct got_object *obj)
119 if (obj->refcnt > 0) {
125 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
126 struct got_delta *delta;
127 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
128 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
129 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
130 got_delta_close(delta);
133 if (obj->flags & GOT_OBJ_FLAG_PACKED)
134 free(obj->path_packfile);
139 got_object_qid_free(struct got_object_qid *qid)
145 struct got_commit_object *
146 got_object_commit_alloc_partial(void)
148 struct got_commit_object *commit;
150 commit = calloc(1, sizeof(*commit));
153 commit->tree_id = malloc(sizeof(*commit->tree_id));
154 if (commit->tree_id == NULL) {
159 SIMPLEQ_INIT(&commit->parent_ids);
164 const struct got_error *
165 got_object_commit_add_parent(struct got_commit_object *commit,
168 const struct got_error *err = NULL;
169 struct got_object_qid *qid;
171 err = got_object_qid_alloc_partial(&qid);
175 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
176 err = got_error(GOT_ERR_BAD_OBJ_DATA);
182 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
188 static const struct got_error *
189 parse_gmtoff(time_t *gmtoff, const char *tzstr)
192 const char *p = tzstr;
200 return got_error(GOT_ERR_BAD_OBJ_DATA);
202 if (!isdigit(*p) && !isdigit(*(p + 1)))
203 return got_error(GOT_ERR_BAD_OBJ_DATA);
204 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
207 if (!isdigit(*p) && !isdigit(*(p + 1)))
208 return got_error(GOT_ERR_BAD_OBJ_DATA);
209 m = ((*p - '0') * 10) + (*(p + 1) - '0');
211 *gmtoff = (h * 60 * 60 + m * 60) * sign;
215 static const struct got_error *
216 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
218 const struct got_error *err = NULL;
222 /* Parse and strip off trailing timezone indicator string. */
223 space = strrchr(committer, ' ');
225 return got_error(GOT_ERR_BAD_OBJ_DATA);
226 tzstr = strdup(space + 1);
228 return got_error_from_errno();
229 err = parse_gmtoff(gmtoff, tzstr);
235 /* Timestamp is separated from committer name + email by space. */
236 space = strrchr(committer, ' ');
238 return got_error(GOT_ERR_BAD_OBJ_DATA);
240 /* Timestamp parsed here is expressed in comitter's local time. */
241 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
243 return got_error(GOT_ERR_BAD_OBJ_DATA);
245 /* Express the time stamp in UTC. */
248 /* Strip off parsed time information, leaving just author and email. */
255 got_object_commit_close(struct got_commit_object *commit)
257 struct got_object_qid *qid;
259 if (commit->refcnt > 0) {
261 if (commit->refcnt > 0)
265 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
266 qid = SIMPLEQ_FIRST(&commit->parent_ids);
267 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
268 got_object_qid_free(qid);
271 free(commit->tree_id);
272 free(commit->author);
273 free(commit->committer);
274 free(commit->logmsg);
278 struct got_object_id *
279 got_object_commit_get_tree_id(struct got_commit_object *commit)
281 return commit->tree_id;
285 got_object_commit_get_nparents(struct got_commit_object *commit)
287 return commit->nparents;
290 const struct got_object_id_queue *
291 got_object_commit_get_parent_ids(struct got_commit_object *commit)
293 return &commit->parent_ids;
297 got_object_commit_get_author(struct got_commit_object *commit)
299 return commit->author;
303 got_object_commit_get_author_time(struct got_commit_object *commit)
305 return commit->author_time;
308 time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
310 return commit->author_gmtoff;
314 got_object_commit_get_committer(struct got_commit_object *commit)
316 return commit->committer;
320 got_object_commit_get_committer_time(struct got_commit_object *commit)
322 return commit->committer_time;
326 got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
328 return commit->committer_gmtoff;
332 got_object_commit_get_logmsg(struct got_commit_object *commit)
334 return commit->logmsg;
337 const struct got_error *
338 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
340 const struct got_error *err = NULL;
343 ssize_t remain = (ssize_t)len;
345 *commit = got_object_commit_alloc_partial();
347 return got_error_from_errno();
349 tlen = strlen(GOT_COMMIT_TAG_TREE);
350 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
352 if (remain < SHA1_DIGEST_STRING_LENGTH) {
353 err = got_error(GOT_ERR_BAD_OBJ_DATA);
357 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
358 err = got_error(GOT_ERR_BAD_OBJ_DATA);
361 remain -= SHA1_DIGEST_STRING_LENGTH;
362 s += SHA1_DIGEST_STRING_LENGTH;
364 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 tlen = strlen(GOT_COMMIT_TAG_PARENT);
369 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
371 if (remain < SHA1_DIGEST_STRING_LENGTH) {
372 err = got_error(GOT_ERR_BAD_OBJ_DATA);
376 err = got_object_commit_add_parent(*commit, s);
380 remain -= SHA1_DIGEST_STRING_LENGTH;
381 s += SHA1_DIGEST_STRING_LENGTH;
384 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
385 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
391 err = got_error(GOT_ERR_BAD_OBJ_DATA);
397 err = got_error(GOT_ERR_BAD_OBJ_DATA);
402 err = parse_commit_time(&(*commit)->author_time,
403 &(*commit)->author_gmtoff, s);
406 (*commit)->author = strdup(s);
407 if ((*commit)->author == NULL) {
408 err = got_error_from_errno();
415 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
416 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
422 err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 err = got_error(GOT_ERR_BAD_OBJ_DATA);
433 err = parse_commit_time(&(*commit)->committer_time,
434 &(*commit)->committer_gmtoff, s);
437 (*commit)->committer = strdup(s);
438 if ((*commit)->committer == NULL) {
439 err = got_error_from_errno();
446 (*commit)->logmsg = strndup(s, remain);
447 if ((*commit)->logmsg == NULL) {
448 err = got_error_from_errno();
453 got_object_commit_close(*commit);
460 got_object_tree_entry_close(struct got_tree_entry *te)
468 got_object_tree_close(struct got_tree_object *tree)
470 struct got_tree_entry *te;
472 if (tree->refcnt > 0) {
474 if (tree->refcnt > 0)
478 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
479 te = SIMPLEQ_FIRST(&tree->entries.head);
480 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
481 got_object_tree_entry_close(te);
487 struct got_tree_entry *
488 got_alloc_tree_entry_partial(void)
490 struct got_tree_entry *te;
492 te = malloc(sizeof(*te));
496 te->id = malloc(sizeof(*te->id));
497 if (te->id == NULL) {
504 static const struct got_error *
505 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
508 char *p = buf, *space;
509 const struct got_error *err = NULL;
511 *te = got_alloc_tree_entry_partial();
513 return got_error_from_errno();
515 *elen = strlen(buf) + 1;
516 if (*elen > maxlen) {
519 return got_error(GOT_ERR_BAD_OBJ_DATA);
522 space = strchr(buf, ' ');
524 err = got_error(GOT_ERR_BAD_OBJ_DATA);
531 if (*p < '0' && *p > '7') {
532 err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 (*te)->mode |= *p - '0';
540 (*te)->name = strdup(space + 1);
541 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
542 err = got_error(GOT_ERR_BAD_OBJ_DATA);
546 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
547 *elen += SHA1_DIGEST_LENGTH;
550 got_object_tree_entry_close(*te);
556 const struct got_error *
557 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
559 const struct got_error *err;
562 *tree = calloc(1, sizeof(**tree));
564 return got_error_from_errno();
566 SIMPLEQ_INIT(&(*tree)->entries.head);
569 struct got_tree_entry *te;
572 err = parse_tree_entry(&te, &elen, buf, remain);
575 (*tree)->entries.nentries++;
576 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
582 got_object_tree_close(*tree);
584 return got_error(GOT_ERR_BAD_OBJ_DATA);
591 got_object_tag_close(struct got_tag_object *tag)
599 const struct got_error *
600 got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
602 const struct got_error *err = NULL;
607 *tag = calloc(1, sizeof(**tag));
609 return got_error_from_errno();
611 tlen = strlen(GOT_TAG_TAG_OBJECT);
612 if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
614 if (remain < SHA1_DIGEST_STRING_LENGTH) {
615 err = got_error(GOT_ERR_BAD_OBJ_DATA);
619 if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
620 err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 remain -= SHA1_DIGEST_STRING_LENGTH;
624 s += SHA1_DIGEST_STRING_LENGTH;
626 err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 err = got_error(GOT_ERR_BAD_OBJ_DATA);
635 tlen = strlen(GOT_TAG_TAG_TYPE);
636 if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
639 err = got_error(GOT_ERR_BAD_OBJ_DATA);
643 if (strncmp(s, GOT_OBJ_TAG_COMMIT,
644 strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
645 (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
646 tlen = strlen(GOT_OBJ_TAG_COMMIT);
649 } else if (strncmp(s, GOT_OBJ_TAG_TREE,
650 strlen(GOT_OBJ_TAG_TREE)) == 0) {
651 (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
652 tlen = strlen(GOT_OBJ_TAG_TREE);
655 } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
656 strlen(GOT_OBJ_TAG_BLOB)) == 0) {
657 (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
658 tlen = strlen(GOT_OBJ_TAG_BLOB);
661 } else if (strncmp(s, GOT_OBJ_TAG_TAG,
662 strlen(GOT_OBJ_TAG_TAG)) == 0) {
663 (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
664 tlen = strlen(GOT_OBJ_TAG_TAG);
668 err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 if (remain <= 0 || *s != '\n') {
673 err = got_error(GOT_ERR_BAD_OBJ_DATA);
679 err = got_error(GOT_ERR_BAD_OBJ_DATA);
683 err = got_error(GOT_ERR_BAD_OBJ_DATA);
687 tlen = strlen(GOT_TAG_TAG_TAG);
688 if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
693 err = got_error(GOT_ERR_BAD_OBJ_DATA);
699 err = got_error(GOT_ERR_BAD_OBJ_DATA);
704 (*tag)->tag = strndup(s, slen);
705 if ((*tag)->tag == NULL) {
706 err = got_error_from_errno();
712 err = got_error(GOT_ERR_BAD_OBJ_DATA);
716 err = got_error(GOT_ERR_BAD_OBJ_DATA);
720 tlen = strlen(GOT_TAG_TAG_TAGGER);
721 if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
727 err = got_error(GOT_ERR_BAD_OBJ_DATA);
733 err = got_error(GOT_ERR_BAD_OBJ_DATA);
738 err = parse_commit_time(&(*tag)->tagger_time,
739 &(*tag)->tagger_gmtoff, s);
742 (*tag)->tagger = strdup(s);
743 if ((*tag)->tagger == NULL) {
744 err = got_error_from_errno();
750 err = got_error(GOT_ERR_BAD_OBJ_DATA);
754 err = got_error(GOT_ERR_BAD_OBJ_DATA);
758 (*tag)->tagmsg = strndup(s, remain);
759 if ((*tag)->tagmsg == NULL) {
760 err = got_error_from_errno();
765 got_object_tag_close(*tag);
771 const struct got_error *
772 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
774 const struct got_error *err = NULL;
775 static const size_t blocksize = 512;
776 size_t n, total, remain;
782 buf = malloc(blocksize);
784 return got_error_from_errno();
791 newbuf = reallocarray(buf, 1, total + blocksize);
792 if (newbuf == NULL) {
793 err = got_error_from_errno();
799 n = fread(buf + total, 1, remain, f);
802 err = got_ferror(f, GOT_ERR_IO);