2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
18 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 a1fd68d8 2018-01-12 stsp #include <errno.h>
21 d71d75ad 2017-11-05 stsp #include <stdio.h>
22 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
23 ab9a70b2 2017-11-06 stsp #include <string.h>
24 d71d75ad 2017-11-05 stsp #include <sha1.h>
25 ab9a70b2 2017-11-06 stsp #include <zlib.h>
26 ab9a70b2 2017-11-06 stsp #include <ctype.h>
27 ab9a70b2 2017-11-06 stsp #include <limits.h>
29 ab9a70b2 2017-11-06 stsp #include "got_error.h"
30 d71d75ad 2017-11-05 stsp #include "got_object.h"
31 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
32 d1cda826 2017-11-06 stsp #include "got_sha1.h"
33 a1fd68d8 2018-01-12 stsp #include "pack.h"
34 96f5e8b3 2018-01-23 stsp #include "delta.h"
35 eef6493a 2018-01-19 stsp #include "object.h"
36 4ca7b755 2018-01-26 stsp #include "zb.h"
39 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
42 ab9a70b2 2017-11-06 stsp #ifndef nitems
43 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
46 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
47 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
48 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
50 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
51 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
52 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
53 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
56 d71d75ad 2017-11-05 stsp got_object_id_str(struct got_object_id *id, char *buf, size_t size)
58 a1fd68d8 2018-01-12 stsp return got_sha1_digest_to_str(id->sha1, buf, size);
62 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
64 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
68 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
70 b107e67f 2018-01-19 stsp switch (obj->type) {
71 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
72 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
73 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
74 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
75 b107e67f 2018-01-19 stsp return obj->type;
81 96f5e8b3 2018-01-23 stsp /* not reached */
85 ab9a70b2 2017-11-06 stsp static const struct got_error *
86 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
88 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
89 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
90 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
91 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
93 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
94 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
95 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
96 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
98 ab9a70b2 2017-11-06 stsp int type = 0;
99 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
101 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
103 ab9a70b2 2017-11-06 stsp if (p == NULL)
104 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
106 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
108 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
109 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
110 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
111 ab9a70b2 2017-11-06 stsp const char *errstr;
113 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
116 ab9a70b2 2017-11-06 stsp type = obj_types[i];
117 63323519 2017-11-06 stsp if (len <= tlen)
118 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
119 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
120 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
121 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
125 ab9a70b2 2017-11-06 stsp if (type == 0)
126 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
128 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
129 1db76ab5 2018-01-26 mpi if (*obj == NULL)
130 1db76ab5 2018-01-26 mpi return got_error(GOT_ERR_NO_MEM);
131 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
132 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
133 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
134 ab9a70b2 2017-11-06 stsp return NULL;
137 ab9a70b2 2017-11-06 stsp static const struct got_error *
138 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
141 ab9a70b2 2017-11-06 stsp const struct got_error *err;
142 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
144 744d9326 2017-12-01 stsp size_t len;
145 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
146 744d9326 2017-12-01 stsp size_t outlen, totlen;
147 ab9a70b2 2017-11-06 stsp int i, ret;
149 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
150 a3e2cbea 2017-12-01 stsp if (buf == NULL)
151 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_NO_MEM);
153 4ca7b755 2018-01-26 stsp err = got_inflate_init(&zb, zbsize);
155 ab9a70b2 2017-11-06 stsp return err;
158 744d9326 2017-12-01 stsp totlen = 0;
160 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
163 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
164 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
165 e302c59e 2017-12-01 stsp if (buf == NULL) {
166 e302c59e 2017-12-01 stsp err = got_error(GOT_ERR_NO_MEM);
170 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
171 744d9326 2017-12-01 stsp totlen += outlen;
173 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
175 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
177 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
178 ab9a70b2 2017-11-06 stsp return err;
181 d1cda826 2017-11-06 stsp static const struct got_error *
182 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
184 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
185 ab9a70b2 2017-11-06 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
186 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
188 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
189 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
191 ab9a70b2 2017-11-06 stsp got_object_id_str(id, hex, sizeof(hex));
193 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
194 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
195 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
197 d1cda826 2017-11-06 stsp free(path_objects);
198 d1cda826 2017-11-06 stsp return err;
201 4ee4114f 2018-01-23 stsp static const struct got_error *
202 eb651edf 2018-02-11 stsp open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
204 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
205 a1fd68d8 2018-01-12 stsp char *path;
207 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
209 d1cda826 2017-11-06 stsp return err;
210 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
211 4558fcd4 2018-01-14 stsp if (*f == NULL) {
212 6c00b545 2018-01-17 stsp err = got_error_from_errno();
216 4558fcd4 2018-01-14 stsp free(path);
217 4558fcd4 2018-01-14 stsp return err;
220 4558fcd4 2018-01-14 stsp const struct got_error *
221 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
222 4558fcd4 2018-01-14 stsp struct got_object_id *id)
224 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
225 6c00b545 2018-01-17 stsp char *path;
228 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
230 4558fcd4 2018-01-14 stsp return err;
232 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
233 6c00b545 2018-01-17 stsp if (f == NULL) {
234 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
235 6c00b545 2018-01-17 stsp err = got_error_from_errno();
238 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
241 6c00b545 2018-01-17 stsp if (*obj == NULL)
242 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
244 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
247 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
250 6c00b545 2018-01-17 stsp free(path);
251 6c00b545 2018-01-17 stsp if (err && f)
253 ab9a70b2 2017-11-06 stsp return err;
258 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
260 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
261 c3703302 2018-01-23 stsp struct got_delta *delta;
262 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
263 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
264 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
265 c3703302 2018-01-23 stsp got_delta_close(delta);
268 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
269 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
274 d1cda826 2017-11-06 stsp commit_object_valid(struct got_commit_object *commit)
279 d1cda826 2017-11-06 stsp if (commit == NULL)
283 d1cda826 2017-11-06 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
284 d1cda826 2017-11-06 stsp if (commit->tree_id.sha1[i] == 0)
287 d1cda826 2017-11-06 stsp if (n == SHA1_DIGEST_LENGTH)
293 d1cda826 2017-11-06 stsp static const struct got_error *
294 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
296 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
297 d1cda826 2017-11-06 stsp char *s = buf;
298 d1cda826 2017-11-06 stsp size_t tlen;
299 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
301 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
302 d1cda826 2017-11-06 stsp if (*commit == NULL)
303 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
305 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
307 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
308 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
309 d1cda826 2017-11-06 stsp remain -= tlen;
310 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
311 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
315 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
316 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
319 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
320 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
322 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
326 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
327 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
328 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
330 d1cda826 2017-11-06 stsp remain -= tlen;
331 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
332 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
337 d1cda826 2017-11-06 stsp if (pid == NULL) {
338 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
342 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(pid->id.sha1, s)) {
343 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
346 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
347 d1cda826 2017-11-06 stsp (*commit)->nparents++;
349 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
350 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
353 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
354 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
357 d1cda826 2017-11-06 stsp remain -= tlen;
358 d1cda826 2017-11-06 stsp if (remain <= 0) {
359 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
363 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
364 d1cda826 2017-11-06 stsp if (p == NULL) {
365 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
369 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
370 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
371 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
374 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
375 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
378 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
379 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
382 d1cda826 2017-11-06 stsp remain -= tlen;
383 d1cda826 2017-11-06 stsp if (remain <= 0) {
384 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
388 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
389 d1cda826 2017-11-06 stsp if (p == NULL) {
390 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
394 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
395 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
396 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
399 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
400 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
403 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
404 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
405 eb651edf 2018-02-11 stsp err = got_error(GOT_ERR_NO_MEM);
410 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
411 0ffeb3c2 2017-11-26 stsp return err;
414 0ffeb3c2 2017-11-26 stsp static void
415 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
417 0ffeb3c2 2017-11-26 stsp free(te->name);
421 0ffeb3c2 2017-11-26 stsp static const struct got_error *
422 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
423 0ffeb3c2 2017-11-26 stsp size_t maxlen)
425 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
426 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
427 0ffeb3c2 2017-11-26 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
429 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
430 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
431 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
433 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
434 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
436 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
439 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
440 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
442 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
444 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
445 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
446 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
449 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
450 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
454 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
455 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
456 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
459 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
460 0ffeb3c2 2017-11-26 stsp memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
461 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
464 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
465 d1cda826 2017-11-06 stsp return err;
468 d1cda826 2017-11-06 stsp static const struct got_error *
469 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
470 0ffeb3c2 2017-11-26 stsp char *buf, size_t len)
472 90356acc 2018-01-27 stsp const struct got_error *err;
473 0ffeb3c2 2017-11-26 stsp size_t remain = len;
474 0ffeb3c2 2017-11-26 stsp int nentries;
476 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
477 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
478 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
480 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
482 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
483 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
484 0ffeb3c2 2017-11-26 stsp size_t elen;
486 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
488 90356acc 2018-01-27 stsp return err;
489 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
490 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
491 0ffeb3c2 2017-11-26 stsp buf += elen;
492 0ffeb3c2 2017-11-26 stsp remain -= elen;
495 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
496 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
497 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
500 0ffeb3c2 2017-11-26 stsp return NULL;
503 0ffeb3c2 2017-11-26 stsp static const struct got_error *
504 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
506 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
507 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
508 eb651edf 2018-02-11 stsp size_t n, total, remain;
509 eb651edf 2018-02-11 stsp uint8_t *buf;
511 eb651edf 2018-02-11 stsp *outbuf = NULL;
512 eb651edf 2018-02-11 stsp *outlen = 0;
514 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
515 eb651edf 2018-02-11 stsp if (buf == NULL)
516 eb651edf 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
518 eb651edf 2018-02-11 stsp remain = blocksize;
520 eb651edf 2018-02-11 stsp while (1) {
521 eb651edf 2018-02-11 stsp if (remain == 0) {
522 eb651edf 2018-02-11 stsp uint8_t *newbuf;
523 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
524 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
525 eb651edf 2018-02-11 stsp err = got_error(GOT_ERR_NO_MEM);
528 eb651edf 2018-02-11 stsp buf = newbuf;
529 eb651edf 2018-02-11 stsp remain += blocksize;
531 eb651edf 2018-02-11 stsp n = fread(buf, 1, remain, f);
532 eb651edf 2018-02-11 stsp if (n == 0) {
533 eb651edf 2018-02-11 stsp if (ferror(f)) {
534 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
537 eb651edf 2018-02-11 stsp break; /* EOF */
539 eb651edf 2018-02-11 stsp remain -= n;
540 eb651edf 2018-02-11 stsp total += n;
544 eb651edf 2018-02-11 stsp if (err == NULL) {
545 eb651edf 2018-02-11 stsp *outbuf = buf;
546 eb651edf 2018-02-11 stsp *outlen = total;
549 eb651edf 2018-02-11 stsp return err;
552 eb651edf 2018-02-11 stsp static const struct got_error *
553 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
554 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
556 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
557 d1cda826 2017-11-06 stsp size_t len;
558 eb651edf 2018-02-11 stsp uint8_t *p;
559 d1cda826 2017-11-06 stsp int i, ret;
561 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
562 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
564 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
566 d1cda826 2017-11-06 stsp return err;
568 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
569 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
573 d1cda826 2017-11-06 stsp /* Skip object header. */
574 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
575 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
578 d1cda826 2017-11-06 stsp return err;
581 d1cda826 2017-11-06 stsp const struct got_error *
582 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
583 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
585 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
588 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
589 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
591 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
592 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&f, obj, repo);
594 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
596 d1cda826 2017-11-06 stsp return err;
598 4558fcd4 2018-01-14 stsp err = read_commit_object(commit, repo, obj, f);
600 d1cda826 2017-11-06 stsp return err;
604 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
606 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
608 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
609 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
610 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
614 d1cda826 2017-11-06 stsp free(commit->author);
615 d1cda826 2017-11-06 stsp free(commit->committer);
616 d1cda826 2017-11-06 stsp free(commit->logmsg);
617 d1cda826 2017-11-06 stsp free(commit);
620 0ffeb3c2 2017-11-26 stsp static const struct got_error *
621 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
622 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
624 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
625 0ffeb3c2 2017-11-26 stsp size_t len;
626 eb651edf 2018-02-11 stsp uint8_t *p;
627 0ffeb3c2 2017-11-26 stsp int i, ret;
629 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
630 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
632 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
634 0ffeb3c2 2017-11-26 stsp return err;
636 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
637 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
641 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
642 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
643 eb651edf 2018-02-11 stsp err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
646 0ffeb3c2 2017-11-26 stsp return err;
649 0ffeb3c2 2017-11-26 stsp const struct got_error *
650 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
651 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
653 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
656 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
657 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
659 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
660 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&f, obj, repo);
662 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
664 0ffeb3c2 2017-11-26 stsp return err;
666 4558fcd4 2018-01-14 stsp err = read_tree_object(tree, repo, obj, f);
668 0ffeb3c2 2017-11-26 stsp return err;
672 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
674 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
676 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
677 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
678 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
679 f715ca7f 2017-11-27 stsp tree_entry_close(te);
682 f715ca7f 2017-11-27 stsp free(tree);
685 68482ea3 2017-11-27 stsp const struct got_error *
686 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
687 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
689 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
691 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
692 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
694 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
695 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
697 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
698 4558fcd4 2018-01-14 stsp if (*blob == NULL)
699 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_NO_MEM);
701 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
702 eb651edf 2018-02-11 stsp (*blob)->read_buf = calloc(1, blocksize);
703 eb651edf 2018-02-11 stsp if ((*blob)->read_buf == NULL)
704 eb651edf 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
705 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
707 eb651edf 2018-02-11 stsp return err;
709 eb651edf 2018-02-11 stsp err = open_loose_object(&((*blob)->f), obj, repo);
711 eb651edf 2018-02-11 stsp free(*blob);
712 eb651edf 2018-02-11 stsp return err;
715 eb651edf 2018-02-11 stsp err = got_inflate_init(&(*blob)->zb, blocksize);
716 eb651edf 2018-02-11 stsp if (err != NULL) {
717 eb651edf 2018-02-11 stsp fclose((*blob)->f);
718 eb651edf 2018-02-11 stsp free(*blob);
719 eb651edf 2018-02-11 stsp return err;
722 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
723 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
726 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
727 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
728 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
730 68482ea3 2017-11-27 stsp return err;
734 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
736 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
737 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
739 eb651edf 2018-02-11 stsp free(blob->read_buf);
740 68482ea3 2017-11-27 stsp fclose(blob->f);
741 68482ea3 2017-11-27 stsp free(blob);
744 68482ea3 2017-11-27 stsp const struct got_error *
745 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
749 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
750 eb651edf 2018-02-11 stsp return got_inflate_read(&blob->zb, blob->f, outlenp);
752 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
753 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
754 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
755 eb651edf 2018-02-11 stsp *outlenp = n;
756 eb651edf 2018-02-11 stsp return NULL;