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 "
65 const struct got_error *
66 got_object_qid_alloc_partial(struct got_object_qid **qid)
68 const struct got_error *err = NULL;
70 *qid = malloc(sizeof(**qid));
72 return got_error_from_errno();
74 (*qid)->id = malloc(sizeof(*((*qid)->id)));
75 if ((*qid)->id == NULL) {
76 err = got_error_from_errno();
77 got_object_qid_free(*qid);
85 const struct got_error *
86 got_object_id_str(char **outbuf, struct got_object_id *id)
88 static const size_t len = SHA1_DIGEST_STRING_LENGTH;
90 *outbuf = malloc(len);
92 return got_error_from_errno();
94 if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
97 return got_error(GOT_ERR_BAD_OBJ_ID_STR);
104 got_object_close(struct got_object *obj)
106 if (obj->refcnt > 0) {
112 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
113 struct got_delta *delta;
114 while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
115 delta = SIMPLEQ_FIRST(&obj->deltas.entries);
116 SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
117 got_delta_close(delta);
120 if (obj->flags & GOT_OBJ_FLAG_PACKED)
121 free(obj->path_packfile);
126 got_object_qid_free(struct got_object_qid *qid)
132 struct got_commit_object *
133 got_object_commit_alloc_partial(void)
135 struct got_commit_object *commit;
137 commit = calloc(1, sizeof(*commit));
140 commit->tree_id = calloc(1, sizeof(*commit->tree_id));
141 if (commit->tree_id == NULL) {
146 SIMPLEQ_INIT(&commit->parent_ids);
151 const struct got_error *
152 got_object_commit_add_parent(struct got_commit_object *commit,
155 const struct got_error *err = NULL;
156 struct got_object_qid *qid;
158 err = got_object_qid_alloc_partial(&qid);
162 if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
163 err = got_error(GOT_ERR_BAD_OBJ_DATA);
169 SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
175 static const struct got_error *
176 parse_gmtoff(time_t *gmtoff, const char *tzstr)
179 const char *p = tzstr;
187 return got_error(GOT_ERR_BAD_OBJ_DATA);
189 if (!isdigit(*p) && !isdigit(*(p + 1)))
190 return got_error(GOT_ERR_BAD_OBJ_DATA);
191 h = (((*p - '0') * 10) + (*(p + 1) - '0'));
194 if (!isdigit(*p) && !isdigit(*(p + 1)))
195 return got_error(GOT_ERR_BAD_OBJ_DATA);
196 m = ((*p - '0') * 10) + (*(p + 1) - '0');
198 *gmtoff = (h * 60 * 60 + m * 60) * sign;
202 static const struct got_error *
203 parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
205 const struct got_error *err = NULL;
209 /* Parse and strip off trailing timezone indicator string. */
210 space = strrchr(committer, ' ');
212 return got_error(GOT_ERR_BAD_OBJ_DATA);
213 tzstr = strdup(space + 1);
215 return got_error_from_errno();
216 err = parse_gmtoff(gmtoff, tzstr);
222 /* Timestamp is separated from committer name + email by space. */
223 space = strrchr(committer, ' ');
225 return got_error(GOT_ERR_BAD_OBJ_DATA);
227 /* Timestamp parsed here is expressed in comitter's local time. */
228 *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
230 return got_error(GOT_ERR_BAD_OBJ_DATA);
232 /* Express the time stamp in UTC. */
235 /* Strip off parsed time information, leaving just author and email. */
242 got_object_commit_close(struct got_commit_object *commit)
244 struct got_object_qid *qid;
246 if (commit->refcnt > 0) {
248 if (commit->refcnt > 0)
252 while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
253 qid = SIMPLEQ_FIRST(&commit->parent_ids);
254 SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
255 got_object_qid_free(qid);
258 free(commit->tree_id);
259 free(commit->author);
260 free(commit->committer);
261 free(commit->logmsg);
265 const struct got_error *
266 got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
268 const struct got_error *err = NULL;
271 ssize_t remain = (ssize_t)len;
273 *commit = got_object_commit_alloc_partial();
275 return got_error_from_errno();
277 tlen = strlen(GOT_COMMIT_TAG_TREE);
278 if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
280 if (remain < SHA1_DIGEST_STRING_LENGTH) {
281 err = got_error(GOT_ERR_BAD_OBJ_DATA);
285 if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
286 err = got_error(GOT_ERR_BAD_OBJ_DATA);
289 remain -= SHA1_DIGEST_STRING_LENGTH;
290 s += SHA1_DIGEST_STRING_LENGTH;
292 err = got_error(GOT_ERR_BAD_OBJ_DATA);
296 tlen = strlen(GOT_COMMIT_TAG_PARENT);
297 while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
299 if (remain < SHA1_DIGEST_STRING_LENGTH) {
300 err = got_error(GOT_ERR_BAD_OBJ_DATA);
304 err = got_object_commit_add_parent(*commit, s);
308 remain -= SHA1_DIGEST_STRING_LENGTH;
309 s += SHA1_DIGEST_STRING_LENGTH;
312 tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
313 if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
319 err = got_error(GOT_ERR_BAD_OBJ_DATA);
325 err = got_error(GOT_ERR_BAD_OBJ_DATA);
330 err = parse_commit_time(&(*commit)->author_time,
331 &(*commit)->author_gmtoff, s);
334 (*commit)->author = strdup(s);
335 if ((*commit)->author == NULL) {
336 err = got_error_from_errno();
343 tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
344 if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
350 err = got_error(GOT_ERR_BAD_OBJ_DATA);
356 err = got_error(GOT_ERR_BAD_OBJ_DATA);
361 err = parse_commit_time(&(*commit)->committer_time,
362 &(*commit)->committer_gmtoff, s);
365 (*commit)->committer = strdup(s);
366 if ((*commit)->committer == NULL) {
367 err = got_error_from_errno();
374 (*commit)->logmsg = strndup(s, remain);
375 if ((*commit)->logmsg == NULL) {
376 err = got_error_from_errno();
381 got_object_commit_close(*commit);
388 got_object_tree_entry_close(struct got_tree_entry *te)
396 got_object_tree_close(struct got_tree_object *tree)
398 struct got_tree_entry *te;
400 if (tree->refcnt > 0) {
402 if (tree->refcnt > 0)
406 while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
407 te = SIMPLEQ_FIRST(&tree->entries.head);
408 SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
409 got_object_tree_entry_close(te);
415 struct got_tree_entry *
416 got_alloc_tree_entry_partial(void)
418 struct got_tree_entry *te;
420 te = malloc(sizeof(*te));
424 te->id = malloc(sizeof(*te->id));
425 if (te->id == NULL) {
432 static const struct got_error *
433 parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
436 char *p = buf, *space;
437 const struct got_error *err = NULL;
439 *te = got_alloc_tree_entry_partial();
441 return got_error_from_errno();
443 *elen = strlen(buf) + 1;
444 if (*elen > maxlen) {
447 return got_error(GOT_ERR_BAD_OBJ_DATA);
450 space = strchr(buf, ' ');
452 err = got_error(GOT_ERR_BAD_OBJ_DATA);
458 if (*p < '0' && *p > '7') {
459 err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 (*te)->mode |= *p - '0';
467 (*te)->name = strdup(space + 1);
468 if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
469 err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 buf += strlen(buf) + 1;
473 memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
474 *elen += SHA1_DIGEST_LENGTH;
477 got_object_tree_entry_close(*te);
483 const struct got_error *
484 got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
486 const struct got_error *err;
489 *tree = calloc(1, sizeof(**tree));
491 return got_error_from_errno();
493 SIMPLEQ_INIT(&(*tree)->entries.head);
496 struct got_tree_entry *te;
499 err = parse_tree_entry(&te, &elen, buf, remain);
502 (*tree)->entries.nentries++;
503 SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
509 got_object_tree_close(*tree);
510 return got_error(GOT_ERR_BAD_OBJ_DATA);
516 const struct got_error *
517 got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
519 const struct got_error *err = NULL;
520 static const size_t blocksize = 512;
521 size_t n, total, remain;
527 buf = malloc(blocksize);
529 return got_error_from_errno();
536 newbuf = reallocarray(buf, 1, total + blocksize);
537 if (newbuf == NULL) {
538 err = got_error_from_errno();
544 n = fread(buf + total, 1, remain, f);
547 err = got_ferror(f, GOT_ERR_IO);