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_COMMIT_TAG_TREE "tree "
57 #define GOT_COMMIT_TAG_PARENT "parent "
58 #define GOT_COMMIT_TAG_AUTHOR "author "
59 #define GOT_COMMIT_TAG_COMMITTER "committer "
62 got_object_id_cmp(const struct got_object_id *id1,
63 const struct got_object_id *id2)
65 return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
68 const struct got_error *
69 got_object_qid_alloc_partial(struct got_object_qid **qid)
71 const struct got_error *err = NULL;
73 *qid = malloc(sizeof(**qid));
75 return got_error_from_errno();
77 (*qid)->id = malloc(sizeof(*((*qid)->id)));
78 if ((*qid)->id == NULL) {
79 err = got_error_from_errno();
80 got_object_qid_free(*qid);
88 const struct got_error *
89 got_object_id_str(char **outbuf, struct got_object_id *id)
91 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
93 *outbuf = malloc(len);
95 return got_error_from_errno();
97 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
100 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
107 got_object_close(struct got_object *obj)
109 if (obj->refcnt > 0) {
115 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
116 struct got_delta *delta;
117 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
118 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
119 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
120 got_delta_close(delta);
123 if (obj->flags & GOT_OBJ_FLAG_PACKED)
124 free(obj->path_packfile);
129 got_object_qid_free(struct got_object_qid *qid)
135 struct got_commit_object *
136 got_object_commit_alloc_partial(void)
138 struct got_commit_object *commit;
140 commit = calloc(1, sizeof(*commit));
143 commit->tree_id = malloc(sizeof(*commit->tree_id));
144 if (commit->tree_id == NULL) {
149 SIMPLEQ_INIT(&commit->parent_ids);
154 const struct got_error *
155 got_object_commit_add_parent(struct got_commit_object *commit,
158 const struct got_error *err = NULL;
159 struct got_object_qid *qid;
161 err = got_object_qid_alloc_partial(&qid);
165 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
166 err = got_error(GOT_ERR_BAD_OBJ_DATA);
172 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
178 static const struct got_error *
179 parse_gmtoff(time_t *gmtoff, const char *tzstr)
182 const char *p = tzstr;
190 return got_error(GOT_ERR_BAD_OBJ_DATA);
192 if (!isdigit(*p) && !isdigit(*(p + 1)))
193 return got_error(GOT_ERR_BAD_OBJ_DATA);
194 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
197 if (!isdigit(*p) && !isdigit(*(p + 1)))
198 return got_error(GOT_ERR_BAD_OBJ_DATA);
199 m = ((*p - '0') * 10) + (*(p + 1) - '0');
201 *gmtoff = (h * 60 * 60 + m * 60) * sign;
205 static const struct got_error *
206 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
208 const struct got_error *err = NULL;
212 /* Parse and strip off trailing timezone indicator string. */
213 space = strrchr(committer, ' ');
215 return got_error(GOT_ERR_BAD_OBJ_DATA);
216 tzstr = strdup(space + 1);
218 return got_error_from_errno();
219 err = parse_gmtoff(gmtoff, tzstr);
225 /* Timestamp is separated from committer name + email by space. */
226 space = strrchr(committer, ' ');
228 return got_error(GOT_ERR_BAD_OBJ_DATA);
230 /* Timestamp parsed here is expressed in comitter's local time. */
231 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
233 return got_error(GOT_ERR_BAD_OBJ_DATA);
235 /* Express the time stamp in UTC. */
238 /* Strip off parsed time information, leaving just author and email. */
245 got_object_commit_close(struct got_commit_object *commit)
247 struct got_object_qid *qid;
249 if (commit->refcnt > 0) {
251 if (commit->refcnt > 0)
255 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
256 qid = SIMPLEQ_FIRST(&commit->parent_ids);
257 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
258 got_object_qid_free(qid);
261 free(commit->tree_id);
262 free(commit->author);
263 free(commit->committer);
264 free(commit->logmsg);
268 const struct got_error *
269 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
271 const struct got_error *err = NULL;
274 ssize_t remain = (ssize_t)len;
276 *commit = got_object_commit_alloc_partial();
278 return got_error_from_errno();
280 tlen = strlen(GOT_COMMIT_TAG_TREE);
281 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
283 if (remain < SHA1_DIGEST_STRING_LENGTH) {
284 err = got_error(GOT_ERR_BAD_OBJ_DATA);
288 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
289 err = got_error(GOT_ERR_BAD_OBJ_DATA);
292 remain -= SHA1_DIGEST_STRING_LENGTH;
293 s += SHA1_DIGEST_STRING_LENGTH;
295 err = got_error(GOT_ERR_BAD_OBJ_DATA);
299 tlen = strlen(GOT_COMMIT_TAG_PARENT);
300 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
302 if (remain < SHA1_DIGEST_STRING_LENGTH) {
303 err = got_error(GOT_ERR_BAD_OBJ_DATA);
307 err = got_object_commit_add_parent(*commit, s);
311 remain -= SHA1_DIGEST_STRING_LENGTH;
312 s += SHA1_DIGEST_STRING_LENGTH;
315 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
316 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
322 err = got_error(GOT_ERR_BAD_OBJ_DATA);
328 err = got_error(GOT_ERR_BAD_OBJ_DATA);
333 err = parse_commit_time(&(*commit)->author_time,
334 &(*commit)->author_gmtoff, s);
337 (*commit)->author = strdup(s);
338 if ((*commit)->author == NULL) {
339 err = got_error_from_errno();
346 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
347 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
353 err = got_error(GOT_ERR_BAD_OBJ_DATA);
359 err = got_error(GOT_ERR_BAD_OBJ_DATA);
364 err = parse_commit_time(&(*commit)->committer_time,
365 &(*commit)->committer_gmtoff, s);
368 (*commit)->committer = strdup(s);
369 if ((*commit)->committer == NULL) {
370 err = got_error_from_errno();
377 (*commit)->logmsg = strndup(s, remain);
378 if ((*commit)->logmsg == NULL) {
379 err = got_error_from_errno();
384 got_object_commit_close(*commit);
391 got_object_tree_entry_close(struct got_tree_entry *te)
399 got_object_tree_close(struct got_tree_object *tree)
401 struct got_tree_entry *te;
403 if (tree->refcnt > 0) {
405 if (tree->refcnt > 0)
409 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
410 te = SIMPLEQ_FIRST(&tree->entries.head);
411 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
412 got_object_tree_entry_close(te);
418 struct got_tree_entry *
419 got_alloc_tree_entry_partial(void)
421 struct got_tree_entry *te;
423 te = malloc(sizeof(*te));
427 te->id = malloc(sizeof(*te->id));
428 if (te->id == NULL) {
435 static const struct got_error *
436 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
439 char *p = buf, *space;
440 const struct got_error *err = NULL;
442 *te = got_alloc_tree_entry_partial();
444 return got_error_from_errno();
446 *elen = strlen(buf) + 1;
447 if (*elen > maxlen) {
450 return got_error(GOT_ERR_BAD_OBJ_DATA);
453 space = strchr(buf, ' ');
455 err = got_error(GOT_ERR_BAD_OBJ_DATA);
462 if (*p < '0' && *p > '7') {
463 err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 (*te)->mode |= *p - '0';
471 (*te)->name = strdup(space + 1);
472 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
473 err = got_error(GOT_ERR_BAD_OBJ_DATA);
477 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
478 *elen += SHA1_DIGEST_LENGTH;
481 got_object_tree_entry_close(*te);
487 const struct got_error *
488 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
490 const struct got_error *err;
493 *tree = calloc(1, sizeof(**tree));
495 return got_error_from_errno();
497 SIMPLEQ_INIT(&(*tree)->entries.head);
500 struct got_tree_entry *te;
503 err = parse_tree_entry(&te, &elen, buf, remain);
506 (*tree)->entries.nentries++;
507 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
513 got_object_tree_close(*tree);
514 return got_error(GOT_ERR_BAD_OBJ_DATA);
520 const struct got_error *
521 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
523 const struct got_error *err = NULL;
524 static const size_t blocksize = 512;
525 size_t n, total, remain;
531 buf = malloc(blocksize);
533 return got_error_from_errno();
540 newbuf = reallocarray(buf, 1, total + blocksize);
541 if (newbuf == NULL) {
542 err = got_error_from_errno();
548 n = fread(buf + total, 1, remain, f);
551 err = got_ferror(f, GOT_ERR_IO);