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_privsep.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_cache.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"
60 #define GOT_COMMIT_TAG_TREE "tree "
61 #define GOT_COMMIT_TAG_PARENT "parent "
62 #define GOT_COMMIT_TAG_AUTHOR "author "
63 #define GOT_COMMIT_TAG_COMMITTER "committer "
66 got_object_id_cmp(const struct got_object_id *id1,
67 const struct got_object_id *id2)
69 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
72 const struct got_error *
73 got_object_qid_alloc_partial(struct got_object_qid **qid)
75 const struct got_error *err = NULL;
77 *qid = malloc(sizeof(**qid));
79 return got_error_from_errno();
81 (*qid)->id = malloc(sizeof(*((*qid)->id)));
82 if ((*qid)->id == NULL) {
83 err = got_error_from_errno();
84 got_object_qid_free(*qid);
92 const struct got_error *
93 got_object_id_str(char **outbuf, struct got_object_id *id)
95 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
97 *outbuf = malloc(len);
99 return got_error_from_errno();
101 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
104 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
111 got_object_close(struct got_object *obj)
113 if (obj->refcnt > 0) {
119 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
120 struct got_delta *delta;
121 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
122 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
123 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
124 got_delta_close(delta);
127 if (obj->flags & GOT_OBJ_FLAG_PACKED)
128 free(obj->path_packfile);
133 got_object_qid_free(struct got_object_qid *qid)
139 struct got_commit_object *
140 got_object_commit_alloc_partial(void)
142 struct got_commit_object *commit;
144 commit = calloc(1, sizeof(*commit));
147 commit->tree_id = malloc(sizeof(*commit->tree_id));
148 if (commit->tree_id == NULL) {
153 SIMPLEQ_INIT(&commit->parent_ids);
158 const struct got_error *
159 got_object_commit_add_parent(struct got_commit_object *commit,
162 const struct got_error *err = NULL;
163 struct got_object_qid *qid;
165 err = got_object_qid_alloc_partial(&qid);
169 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
170 err = got_error(GOT_ERR_BAD_OBJ_DATA);
176 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
182 static const struct got_error *
183 parse_gmtoff(time_t *gmtoff, const char *tzstr)
186 const char *p = tzstr;
194 return got_error(GOT_ERR_BAD_OBJ_DATA);
196 if (!isdigit(*p) && !isdigit(*(p + 1)))
197 return got_error(GOT_ERR_BAD_OBJ_DATA);
198 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
201 if (!isdigit(*p) && !isdigit(*(p + 1)))
202 return got_error(GOT_ERR_BAD_OBJ_DATA);
203 m = ((*p - '0') * 10) + (*(p + 1) - '0');
205 *gmtoff = (h * 60 * 60 + m * 60) * sign;
209 static const struct got_error *
210 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
212 const struct got_error *err = NULL;
216 /* Parse and strip off trailing timezone indicator string. */
217 space = strrchr(committer, ' ');
219 return got_error(GOT_ERR_BAD_OBJ_DATA);
220 tzstr = strdup(space + 1);
222 return got_error_from_errno();
223 err = parse_gmtoff(gmtoff, tzstr);
229 /* Timestamp is separated from committer name + email by space. */
230 space = strrchr(committer, ' ');
232 return got_error(GOT_ERR_BAD_OBJ_DATA);
234 /* Timestamp parsed here is expressed in comitter's local time. */
235 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
237 return got_error(GOT_ERR_BAD_OBJ_DATA);
239 /* Express the time stamp in UTC. */
242 /* Strip off parsed time information, leaving just author and email. */
249 got_object_commit_close(struct got_commit_object *commit)
251 struct got_object_qid *qid;
253 if (commit->refcnt > 0) {
255 if (commit->refcnt > 0)
259 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
260 qid = SIMPLEQ_FIRST(&commit->parent_ids);
261 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
262 got_object_qid_free(qid);
265 free(commit->tree_id);
266 free(commit->author);
267 free(commit->committer);
268 free(commit->logmsg);
272 const struct got_error *
273 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
275 const struct got_error *err = NULL;
278 ssize_t remain = (ssize_t)len;
280 *commit = got_object_commit_alloc_partial();
282 return got_error_from_errno();
284 tlen = strlen(GOT_COMMIT_TAG_TREE);
285 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
287 if (remain < SHA1_DIGEST_STRING_LENGTH) {
288 err = got_error(GOT_ERR_BAD_OBJ_DATA);
292 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
293 err = got_error(GOT_ERR_BAD_OBJ_DATA);
296 remain -= SHA1_DIGEST_STRING_LENGTH;
297 s += SHA1_DIGEST_STRING_LENGTH;
299 err = got_error(GOT_ERR_BAD_OBJ_DATA);
303 tlen = strlen(GOT_COMMIT_TAG_PARENT);
304 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
306 if (remain < SHA1_DIGEST_STRING_LENGTH) {
307 err = got_error(GOT_ERR_BAD_OBJ_DATA);
311 err = got_object_commit_add_parent(*commit, s);
315 remain -= SHA1_DIGEST_STRING_LENGTH;
316 s += SHA1_DIGEST_STRING_LENGTH;
319 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
320 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
326 err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 err = got_error(GOT_ERR_BAD_OBJ_DATA);
337 err = parse_commit_time(&(*commit)->author_time,
338 &(*commit)->author_gmtoff, s);
341 (*commit)->author = strdup(s);
342 if ((*commit)->author == NULL) {
343 err = got_error_from_errno();
350 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
351 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
357 err = got_error(GOT_ERR_BAD_OBJ_DATA);
363 err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 err = parse_commit_time(&(*commit)->committer_time,
369 &(*commit)->committer_gmtoff, s);
372 (*commit)->committer = strdup(s);
373 if ((*commit)->committer == NULL) {
374 err = got_error_from_errno();
381 (*commit)->logmsg = strndup(s, remain);
382 if ((*commit)->logmsg == NULL) {
383 err = got_error_from_errno();
388 got_object_commit_close(*commit);
395 got_object_tree_entry_close(struct got_tree_entry *te)
403 got_object_tree_close(struct got_tree_object *tree)
405 struct got_tree_entry *te;
407 if (tree->refcnt > 0) {
409 if (tree->refcnt > 0)
413 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
414 te = SIMPLEQ_FIRST(&tree->entries.head);
415 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
416 got_object_tree_entry_close(te);
422 struct got_tree_entry *
423 got_alloc_tree_entry_partial(void)
425 struct got_tree_entry *te;
427 te = malloc(sizeof(*te));
431 te->id = malloc(sizeof(*te->id));
432 if (te->id == NULL) {
439 static const struct got_error *
440 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
443 char *p = buf, *space;
444 const struct got_error *err = NULL;
446 *te = got_alloc_tree_entry_partial();
448 return got_error_from_errno();
450 *elen = strlen(buf) + 1;
451 if (*elen > maxlen) {
454 return got_error(GOT_ERR_BAD_OBJ_DATA);
457 space = strchr(buf, ' ');
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 if (*p < '0' && *p > '7') {
467 err = got_error(GOT_ERR_BAD_OBJ_DATA);
471 (*te)->mode |= *p - '0';
475 (*te)->name = strdup(space + 1);
476 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
477 err = got_error(GOT_ERR_BAD_OBJ_DATA);
481 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
482 *elen += SHA1_DIGEST_LENGTH;
485 got_object_tree_entry_close(*te);
491 const struct got_error *
492 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
494 const struct got_error *err;
497 *tree = calloc(1, sizeof(**tree));
499 return got_error_from_errno();
501 SIMPLEQ_INIT(&(*tree)->entries.head);
504 struct got_tree_entry *te;
507 err = parse_tree_entry(&te, &elen, buf, remain);
510 (*tree)->entries.nentries++;
511 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
517 got_object_tree_close(*tree);
518 return got_error(GOT_ERR_BAD_OBJ_DATA);
524 const struct got_error *
525 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
527 const struct got_error *err = NULL;
528 static const size_t blocksize = 512;
529 size_t n, total, remain;
535 buf = malloc(blocksize);
537 return got_error_from_errno();
544 newbuf = reallocarray(buf, 1, total + blocksize);
545 if (newbuf == NULL) {
546 err = got_error_from_errno();
552 n = fread(buf + total, 1, remain, f);
555 err = got_ferror(f, GOT_ERR_IO);