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 885d3e02 2018-01-27 stsp err = got_inflate_read(&zb, f, NULL, &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 4ee4114f 2018-01-23 stsp fopen_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 3ee5fc21 2018-01-17 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
208 3ee5fc21 2018-01-17 stsp return got_packfile_extract_object(f, obj, repo);
210 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
212 d1cda826 2017-11-06 stsp return err;
213 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
214 4558fcd4 2018-01-14 stsp if (*f == NULL) {
215 6c00b545 2018-01-17 stsp err = got_error_from_errno();
219 4558fcd4 2018-01-14 stsp free(path);
220 4558fcd4 2018-01-14 stsp return err;
223 4558fcd4 2018-01-14 stsp const struct got_error *
224 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
225 4558fcd4 2018-01-14 stsp struct got_object_id *id)
227 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
228 6c00b545 2018-01-17 stsp char *path;
231 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
233 4558fcd4 2018-01-14 stsp return err;
235 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
236 6c00b545 2018-01-17 stsp if (f == NULL) {
237 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
238 6c00b545 2018-01-17 stsp err = got_error_from_errno();
241 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
244 6c00b545 2018-01-17 stsp if (*obj == NULL)
245 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
247 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
250 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
253 6c00b545 2018-01-17 stsp free(path);
254 6c00b545 2018-01-17 stsp if (err && f)
256 ab9a70b2 2017-11-06 stsp return err;
261 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
263 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
264 c3703302 2018-01-23 stsp struct got_delta *delta;
265 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
266 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
267 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
268 c3703302 2018-01-23 stsp got_delta_close(delta);
271 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
272 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
277 d1cda826 2017-11-06 stsp commit_object_valid(struct got_commit_object *commit)
282 d1cda826 2017-11-06 stsp if (commit == NULL)
286 d1cda826 2017-11-06 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
287 d1cda826 2017-11-06 stsp if (commit->tree_id.sha1[i] == 0)
290 d1cda826 2017-11-06 stsp if (n == SHA1_DIGEST_LENGTH)
296 d1cda826 2017-11-06 stsp static const struct got_error *
297 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
299 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
300 d1cda826 2017-11-06 stsp char *s = buf;
301 d1cda826 2017-11-06 stsp size_t tlen;
302 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
304 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
305 d1cda826 2017-11-06 stsp if (*commit == NULL)
306 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
308 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
310 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
311 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
312 d1cda826 2017-11-06 stsp remain -= tlen;
313 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
314 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
318 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
319 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
322 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
323 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
325 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
329 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
330 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
331 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
333 d1cda826 2017-11-06 stsp remain -= tlen;
334 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
335 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
340 d1cda826 2017-11-06 stsp if (pid == NULL) {
341 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
345 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(pid->id.sha1, s)) {
346 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
350 d1cda826 2017-11-06 stsp (*commit)->nparents++;
352 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
355 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
356 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
359 d1cda826 2017-11-06 stsp remain -= tlen;
360 d1cda826 2017-11-06 stsp if (remain <= 0) {
361 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
365 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
366 d1cda826 2017-11-06 stsp if (p == NULL) {
367 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
371 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
372 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
373 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
376 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
379 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
380 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
383 d1cda826 2017-11-06 stsp remain -= tlen;
384 d1cda826 2017-11-06 stsp if (remain <= 0) {
385 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
390 d1cda826 2017-11-06 stsp if (p == NULL) {
391 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
396 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
397 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
400 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
403 d1cda826 2017-11-06 stsp (*commit)->logmsg = strdup(s);
406 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
407 0ffeb3c2 2017-11-26 stsp return err;
410 0ffeb3c2 2017-11-26 stsp static void
411 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
413 0ffeb3c2 2017-11-26 stsp free(te->name);
417 0ffeb3c2 2017-11-26 stsp static const struct got_error *
418 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
419 0ffeb3c2 2017-11-26 stsp size_t maxlen)
421 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
422 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
423 0ffeb3c2 2017-11-26 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
425 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
426 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
427 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
429 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
430 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
432 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
435 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
436 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
438 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
440 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
441 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
442 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
445 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
446 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
450 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
451 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
452 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
455 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
456 0ffeb3c2 2017-11-26 stsp memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
457 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
460 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
461 d1cda826 2017-11-06 stsp return err;
464 d1cda826 2017-11-06 stsp static const struct got_error *
465 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
466 0ffeb3c2 2017-11-26 stsp char *buf, size_t len)
468 90356acc 2018-01-27 stsp const struct got_error *err;
469 0ffeb3c2 2017-11-26 stsp size_t remain = len;
470 0ffeb3c2 2017-11-26 stsp int nentries;
472 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
473 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
474 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
476 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
478 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
479 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
480 0ffeb3c2 2017-11-26 stsp size_t elen;
482 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
484 90356acc 2018-01-27 stsp return err;
485 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
486 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
487 0ffeb3c2 2017-11-26 stsp buf += elen;
488 0ffeb3c2 2017-11-26 stsp remain -= elen;
491 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
492 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
493 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
496 0ffeb3c2 2017-11-26 stsp return NULL;
499 0ffeb3c2 2017-11-26 stsp static const struct got_error *
500 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
501 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
503 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
504 d1cda826 2017-11-06 stsp struct got_zstream_buf zb;
505 d1cda826 2017-11-06 stsp size_t len;
507 d1cda826 2017-11-06 stsp int i, ret;
509 4ca7b755 2018-01-26 stsp err = got_inflate_init(&zb, 8192);
511 d1cda826 2017-11-06 stsp return err;
513 c743ac7d 2018-01-27 stsp err = got_inflate_read(&zb, f, NULL, &len);
515 c743ac7d 2018-01-27 stsp return err;
517 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
518 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
522 d1cda826 2017-11-06 stsp /* Skip object header. */
523 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
524 d1cda826 2017-11-06 stsp err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
526 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
527 d1cda826 2017-11-06 stsp return err;
530 d1cda826 2017-11-06 stsp const struct got_error *
531 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
532 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
534 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
537 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
538 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
540 4ee4114f 2018-01-23 stsp err = fopen_object(&f, obj, repo);
542 d1cda826 2017-11-06 stsp return err;
544 4558fcd4 2018-01-14 stsp err = read_commit_object(commit, repo, obj, f);
546 d1cda826 2017-11-06 stsp return err;
550 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
552 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
554 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
555 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
556 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
560 d1cda826 2017-11-06 stsp free(commit->author);
561 d1cda826 2017-11-06 stsp free(commit->committer);
562 d1cda826 2017-11-06 stsp free(commit->logmsg);
563 d1cda826 2017-11-06 stsp free(commit);
566 0ffeb3c2 2017-11-26 stsp static const struct got_error *
567 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
568 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
570 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
571 0ffeb3c2 2017-11-26 stsp struct got_zstream_buf zb;
572 0ffeb3c2 2017-11-26 stsp size_t len;
574 0ffeb3c2 2017-11-26 stsp int i, ret;
576 4ca7b755 2018-01-26 stsp err = got_inflate_init(&zb, 8192);
578 0ffeb3c2 2017-11-26 stsp return err;
580 c743ac7d 2018-01-27 stsp err = got_inflate_read(&zb, f, NULL, &len);
582 c743ac7d 2018-01-27 stsp return err;
584 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
585 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
589 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
590 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
591 0ffeb3c2 2017-11-26 stsp err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
593 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
594 0ffeb3c2 2017-11-26 stsp return err;
597 0ffeb3c2 2017-11-26 stsp const struct got_error *
598 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
599 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
601 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
604 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
605 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
607 4ee4114f 2018-01-23 stsp err = fopen_object(&f, obj, repo);
609 0ffeb3c2 2017-11-26 stsp return err;
611 4558fcd4 2018-01-14 stsp err = read_tree_object(tree, repo, obj, f);
613 0ffeb3c2 2017-11-26 stsp return err;
617 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
619 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
621 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
622 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
623 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
624 f715ca7f 2017-11-27 stsp tree_entry_close(te);
627 f715ca7f 2017-11-27 stsp free(tree);
630 68482ea3 2017-11-27 stsp const struct got_error *
631 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
632 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
634 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
636 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
637 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
639 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
640 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
642 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
643 4558fcd4 2018-01-14 stsp if (*blob == NULL)
644 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_NO_MEM);
646 4ee4114f 2018-01-23 stsp err = fopen_object(&((*blob)->f), obj, repo);
648 68482ea3 2017-11-27 stsp free(*blob);
649 4558fcd4 2018-01-14 stsp return err;
652 4ca7b755 2018-01-26 stsp err = got_inflate_init(&(*blob)->zb, blocksize);
653 68482ea3 2017-11-27 stsp if (err != NULL) {
654 68482ea3 2017-11-27 stsp fclose((*blob)->f);
655 68482ea3 2017-11-27 stsp free(*blob);
656 68482ea3 2017-11-27 stsp return err;
659 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
660 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
662 68482ea3 2017-11-27 stsp return err;
666 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
668 4ca7b755 2018-01-26 stsp got_inflate_end(&blob->zb);
669 68482ea3 2017-11-27 stsp fclose(blob->f);
670 68482ea3 2017-11-27 stsp free(blob);
673 68482ea3 2017-11-27 stsp const struct got_error *
674 68482ea3 2017-11-27 stsp got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
676 885d3e02 2018-01-27 stsp return got_inflate_read(&blob->zb, blob->f, NULL, outlenp);