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 eef6493a 2018-01-19 stsp #include "object.h"
37 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
40 ab9a70b2 2017-11-06 stsp #ifndef nitems
41 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
44 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
45 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
46 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
48 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
49 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
50 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
51 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
54 d71d75ad 2017-11-05 stsp got_object_id_str(struct got_object_id *id, char *buf, size_t size)
56 a1fd68d8 2018-01-12 stsp return got_sha1_digest_to_str(id->sha1, buf, size);
60 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
62 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
66 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
68 b107e67f 2018-01-19 stsp switch (obj->type) {
69 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
70 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
71 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
72 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
73 b107e67f 2018-01-19 stsp return obj->type;
74 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
75 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
76 b107e67f 2018-01-19 stsp return obj->base_type;
83 ab9a70b2 2017-11-06 stsp inflate_end(struct got_zstream_buf *zb)
85 ab9a70b2 2017-11-06 stsp free(zb->inbuf);
86 ab9a70b2 2017-11-06 stsp free(zb->outbuf);
87 ab9a70b2 2017-11-06 stsp inflateEnd(&zb->z);
90 ab9a70b2 2017-11-06 stsp static const struct got_error *
91 ab9a70b2 2017-11-06 stsp inflate_init(struct got_zstream_buf *zb, size_t bufsize)
93 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
95 ab9a70b2 2017-11-06 stsp memset(zb, 0, sizeof(*zb));
97 ab9a70b2 2017-11-06 stsp zb->z.zalloc = Z_NULL;
98 ab9a70b2 2017-11-06 stsp zb->z.zfree = Z_NULL;
99 ab9a70b2 2017-11-06 stsp if (inflateInit(&zb->z) != Z_OK) {
100 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_IO);
104 ab9a70b2 2017-11-06 stsp zb->inlen = zb->outlen = bufsize;
106 ab9a70b2 2017-11-06 stsp zb->inbuf = calloc(1, zb->inlen);
107 ab9a70b2 2017-11-06 stsp if (zb->inbuf == NULL) {
108 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
112 ab9a70b2 2017-11-06 stsp zb->outbuf = calloc(1, zb->outlen);
113 ab9a70b2 2017-11-06 stsp if (zb->outbuf == NULL) {
114 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
120 ab9a70b2 2017-11-06 stsp inflate_end(zb);
121 ab9a70b2 2017-11-06 stsp return err;
124 ab9a70b2 2017-11-06 stsp static const struct got_error *
125 ab9a70b2 2017-11-06 stsp inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
127 ab9a70b2 2017-11-06 stsp size_t last_total_out = zb->z.total_out;
128 ab9a70b2 2017-11-06 stsp z_stream *z = &zb->z;
129 ab9a70b2 2017-11-06 stsp int n, ret;
131 ab9a70b2 2017-11-06 stsp z->next_out = zb->outbuf;
132 ab9a70b2 2017-11-06 stsp z->avail_out = zb->outlen;
135 a3e2cbea 2017-12-01 stsp if (z->avail_in == 0) {
137 a3e2cbea 2017-12-01 stsp n = fread(zb->inbuf, 1, zb->inlen, f);
138 a3e2cbea 2017-12-01 stsp if (n == 0) {
139 a3e2cbea 2017-12-01 stsp if (ferror(f))
140 6c00b545 2018-01-17 stsp return got_ferror(f, GOT_ERR_IO);
141 a3e2cbea 2017-12-01 stsp *outlenp = 0;
142 a3e2cbea 2017-12-01 stsp return NULL;
144 a3e2cbea 2017-12-01 stsp z->next_in = zb->inbuf;
145 a3e2cbea 2017-12-01 stsp z->avail_in = n;
147 a3e2cbea 2017-12-01 stsp ret = inflate(z, Z_SYNC_FLUSH);
148 a3e2cbea 2017-12-01 stsp } while (ret == Z_OK && z->avail_out > 0);
150 a3e2cbea 2017-12-01 stsp if (ret != Z_OK) {
151 a3e2cbea 2017-12-01 stsp if (ret != Z_STREAM_END)
152 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_DECOMPRESSION);
153 a3e2cbea 2017-12-01 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
156 ab9a70b2 2017-11-06 stsp *outlenp = z->total_out - last_total_out;
157 ab9a70b2 2017-11-06 stsp return NULL;
160 ab9a70b2 2017-11-06 stsp static const struct got_error *
161 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
163 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
164 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
165 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
166 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
168 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
169 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
170 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
171 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
173 ab9a70b2 2017-11-06 stsp int type = 0;
174 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
176 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
178 ab9a70b2 2017-11-06 stsp if (p == NULL)
179 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
181 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
183 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
184 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
185 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
186 ab9a70b2 2017-11-06 stsp const char *errstr;
188 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
191 ab9a70b2 2017-11-06 stsp type = obj_types[i];
192 63323519 2017-11-06 stsp if (len <= tlen)
193 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
194 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
195 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
196 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
200 ab9a70b2 2017-11-06 stsp if (type == 0)
201 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
203 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
204 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
205 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
206 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
207 ab9a70b2 2017-11-06 stsp return NULL;
210 ab9a70b2 2017-11-06 stsp static const struct got_error *
211 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
214 ab9a70b2 2017-11-06 stsp const struct got_error *err;
215 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
217 744d9326 2017-12-01 stsp size_t len;
218 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
219 744d9326 2017-12-01 stsp size_t outlen, totlen;
220 ab9a70b2 2017-11-06 stsp int i, ret;
222 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
223 a3e2cbea 2017-12-01 stsp if (buf == NULL)
224 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_NO_MEM);
226 a3e2cbea 2017-12-01 stsp err = inflate_init(&zb, zbsize);
228 ab9a70b2 2017-11-06 stsp return err;
231 744d9326 2017-12-01 stsp totlen = 0;
233 a3e2cbea 2017-12-01 stsp err = inflate_read(&zb, f, &outlen);
236 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
237 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
238 e302c59e 2017-12-01 stsp if (buf == NULL) {
239 e302c59e 2017-12-01 stsp err = got_error(GOT_ERR_NO_MEM);
243 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
244 744d9326 2017-12-01 stsp totlen += outlen;
246 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
248 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
250 ab9a70b2 2017-11-06 stsp inflate_end(&zb);
251 ab9a70b2 2017-11-06 stsp return err;
254 d1cda826 2017-11-06 stsp static const struct got_error *
255 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
257 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
258 ab9a70b2 2017-11-06 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
259 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
261 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
262 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
264 ab9a70b2 2017-11-06 stsp got_object_id_str(id, hex, sizeof(hex));
266 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
267 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
268 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
270 d1cda826 2017-11-06 stsp free(path_objects);
271 d1cda826 2017-11-06 stsp return err;
274 6c00b545 2018-01-17 stsp const struct got_error *
275 6c00b545 2018-01-17 stsp open_object(FILE **f, struct got_object *obj, struct got_repository *repo)
277 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
278 a1fd68d8 2018-01-12 stsp char *path;
280 3ee5fc21 2018-01-17 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
281 3ee5fc21 2018-01-17 stsp return got_packfile_extract_object(f, obj, repo);
283 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
285 d1cda826 2017-11-06 stsp return err;
286 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
287 4558fcd4 2018-01-14 stsp if (*f == NULL) {
288 6c00b545 2018-01-17 stsp err = got_error_from_errno();
292 4558fcd4 2018-01-14 stsp free(path);
293 4558fcd4 2018-01-14 stsp return err;
296 4558fcd4 2018-01-14 stsp const struct got_error *
297 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
298 4558fcd4 2018-01-14 stsp struct got_object_id *id)
300 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
301 6c00b545 2018-01-17 stsp char *path;
304 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
306 4558fcd4 2018-01-14 stsp return err;
308 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
309 6c00b545 2018-01-17 stsp if (f == NULL) {
310 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
311 6c00b545 2018-01-17 stsp err = got_error_from_errno();
314 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
317 6c00b545 2018-01-17 stsp if (*obj == NULL)
318 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
320 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
323 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
326 6c00b545 2018-01-17 stsp free(path);
327 6c00b545 2018-01-17 stsp if (err && f)
329 ab9a70b2 2017-11-06 stsp return err;
334 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
336 6c00b545 2018-01-17 stsp free(obj->path_packfile);
341 d1cda826 2017-11-06 stsp commit_object_valid(struct got_commit_object *commit)
346 d1cda826 2017-11-06 stsp if (commit == NULL)
350 d1cda826 2017-11-06 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
351 d1cda826 2017-11-06 stsp if (commit->tree_id.sha1[i] == 0)
354 d1cda826 2017-11-06 stsp if (n == SHA1_DIGEST_LENGTH)
360 d1cda826 2017-11-06 stsp static const struct got_error *
361 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
363 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
364 d1cda826 2017-11-06 stsp char *s = buf;
365 d1cda826 2017-11-06 stsp size_t tlen;
366 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
368 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
369 d1cda826 2017-11-06 stsp if (*commit == NULL)
370 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
372 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
374 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
375 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
376 d1cda826 2017-11-06 stsp remain -= tlen;
377 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
378 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
382 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
383 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
386 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
387 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
389 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
393 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
394 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
395 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
397 d1cda826 2017-11-06 stsp remain -= tlen;
398 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
399 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
404 d1cda826 2017-11-06 stsp if (pid == NULL) {
405 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
409 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(pid->id.sha1, s)) {
410 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
413 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
414 d1cda826 2017-11-06 stsp (*commit)->nparents++;
416 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
419 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
420 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
423 d1cda826 2017-11-06 stsp remain -= tlen;
424 d1cda826 2017-11-06 stsp if (remain <= 0) {
425 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
430 d1cda826 2017-11-06 stsp if (p == NULL) {
431 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
435 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
436 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
437 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
440 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
443 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
444 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
447 d1cda826 2017-11-06 stsp remain -= tlen;
448 d1cda826 2017-11-06 stsp if (remain <= 0) {
449 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
453 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
454 d1cda826 2017-11-06 stsp if (p == NULL) {
455 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
459 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
460 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
461 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
464 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
467 d1cda826 2017-11-06 stsp (*commit)->logmsg = strdup(s);
470 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
471 0ffeb3c2 2017-11-26 stsp return err;
474 0ffeb3c2 2017-11-26 stsp static void
475 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
477 0ffeb3c2 2017-11-26 stsp free(te->name);
481 0ffeb3c2 2017-11-26 stsp static const struct got_error *
482 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
483 0ffeb3c2 2017-11-26 stsp size_t maxlen)
485 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
486 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
487 0ffeb3c2 2017-11-26 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
489 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
490 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
491 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
493 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
494 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
496 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
499 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
500 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
502 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
504 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
505 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
506 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
510 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
514 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
515 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
516 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
519 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
520 0ffeb3c2 2017-11-26 stsp memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
521 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
524 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
525 d1cda826 2017-11-06 stsp return err;
528 d1cda826 2017-11-06 stsp static const struct got_error *
529 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
530 0ffeb3c2 2017-11-26 stsp char *buf, size_t len)
532 0ffeb3c2 2017-11-26 stsp size_t remain = len;
533 0ffeb3c2 2017-11-26 stsp int nentries;
535 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
536 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
537 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
539 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
541 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
542 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
543 0ffeb3c2 2017-11-26 stsp size_t elen;
545 0ffeb3c2 2017-11-26 stsp parse_tree_entry(&te, &elen, buf, remain);
546 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
547 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
548 0ffeb3c2 2017-11-26 stsp buf += elen;
549 0ffeb3c2 2017-11-26 stsp remain -= elen;
552 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
553 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
554 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
557 0ffeb3c2 2017-11-26 stsp return NULL;
560 0ffeb3c2 2017-11-26 stsp static const struct got_error *
561 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
562 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
564 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
565 d1cda826 2017-11-06 stsp struct got_zstream_buf zb;
566 d1cda826 2017-11-06 stsp size_t len;
568 d1cda826 2017-11-06 stsp int i, ret;
570 d1cda826 2017-11-06 stsp err = inflate_init(&zb, 8192);
572 d1cda826 2017-11-06 stsp return err;
575 d1cda826 2017-11-06 stsp err = inflate_read(&zb, f, &len);
576 d1cda826 2017-11-06 stsp if (err || len == 0)
578 d1cda826 2017-11-06 stsp } while (len < obj->hdrlen + obj->size);
580 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
581 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
585 d1cda826 2017-11-06 stsp /* Skip object header. */
586 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
587 d1cda826 2017-11-06 stsp err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
589 d1cda826 2017-11-06 stsp inflate_end(&zb);
590 d1cda826 2017-11-06 stsp return err;
593 d1cda826 2017-11-06 stsp const struct got_error *
594 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
595 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
597 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
600 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
601 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
603 6c00b545 2018-01-17 stsp err = open_object(&f, obj, repo);
605 d1cda826 2017-11-06 stsp return err;
607 4558fcd4 2018-01-14 stsp err = read_commit_object(commit, repo, obj, f);
609 d1cda826 2017-11-06 stsp return err;
613 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
615 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
617 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
618 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
619 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
623 d1cda826 2017-11-06 stsp free(commit->author);
624 d1cda826 2017-11-06 stsp free(commit->committer);
625 d1cda826 2017-11-06 stsp free(commit->logmsg);
626 d1cda826 2017-11-06 stsp free(commit);
629 0ffeb3c2 2017-11-26 stsp static const struct got_error *
630 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
631 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
633 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
634 0ffeb3c2 2017-11-26 stsp struct got_zstream_buf zb;
635 0ffeb3c2 2017-11-26 stsp size_t len;
637 0ffeb3c2 2017-11-26 stsp int i, ret;
639 0ffeb3c2 2017-11-26 stsp err = inflate_init(&zb, 8192);
641 0ffeb3c2 2017-11-26 stsp return err;
644 0ffeb3c2 2017-11-26 stsp err = inflate_read(&zb, f, &len);
645 0ffeb3c2 2017-11-26 stsp if (err || len == 0)
647 0ffeb3c2 2017-11-26 stsp } while (len < obj->hdrlen + obj->size);
649 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
650 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
654 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
655 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
656 0ffeb3c2 2017-11-26 stsp err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
658 0ffeb3c2 2017-11-26 stsp inflate_end(&zb);
659 0ffeb3c2 2017-11-26 stsp return err;
662 0ffeb3c2 2017-11-26 stsp const struct got_error *
663 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
664 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
666 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
669 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
670 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
672 6c00b545 2018-01-17 stsp err = open_object(&f, obj, repo);
674 0ffeb3c2 2017-11-26 stsp return err;
676 4558fcd4 2018-01-14 stsp err = read_tree_object(tree, repo, obj, f);
678 0ffeb3c2 2017-11-26 stsp return err;
682 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
684 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
686 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
687 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
688 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
689 f715ca7f 2017-11-27 stsp tree_entry_close(te);
692 f715ca7f 2017-11-27 stsp free(tree);
695 68482ea3 2017-11-27 stsp const struct got_error *
696 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
697 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
699 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
701 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
702 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
704 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
705 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
707 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
708 4558fcd4 2018-01-14 stsp if (*blob == NULL)
709 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_NO_MEM);
711 6c00b545 2018-01-17 stsp err = open_object(&((*blob)->f), obj, repo);
713 68482ea3 2017-11-27 stsp free(*blob);
714 4558fcd4 2018-01-14 stsp return err;
717 68482ea3 2017-11-27 stsp err = inflate_init(&(*blob)->zb, blocksize);
718 68482ea3 2017-11-27 stsp if (err != NULL) {
719 68482ea3 2017-11-27 stsp fclose((*blob)->f);
720 68482ea3 2017-11-27 stsp free(*blob);
721 68482ea3 2017-11-27 stsp return err;
724 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
725 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
727 68482ea3 2017-11-27 stsp return err;
731 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
733 68482ea3 2017-11-27 stsp inflate_end(&blob->zb);
734 68482ea3 2017-11-27 stsp fclose(blob->f);
735 68482ea3 2017-11-27 stsp free(blob);
738 68482ea3 2017-11-27 stsp const struct got_error *
739 68482ea3 2017-11-27 stsp got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
741 68482ea3 2017-11-27 stsp return inflate_read(&blob->zb, blob->f, outlenp);