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"
38 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 ab9a70b2 2017-11-06 stsp #ifndef nitems
42 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
46 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
47 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
49 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
50 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
51 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
52 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
55 d71d75ad 2017-11-05 stsp got_object_id_str(struct got_object_id *id, char *buf, size_t size)
57 a1fd68d8 2018-01-12 stsp return got_sha1_digest_to_str(id->sha1, buf, size);
61 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
63 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
67 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
69 b107e67f 2018-01-19 stsp switch (obj->type) {
70 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
71 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
72 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
73 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
74 b107e67f 2018-01-19 stsp return obj->type;
80 96f5e8b3 2018-01-23 stsp /* not reached */
85 ab9a70b2 2017-11-06 stsp inflate_end(struct got_zstream_buf *zb)
87 ab9a70b2 2017-11-06 stsp free(zb->inbuf);
88 ab9a70b2 2017-11-06 stsp free(zb->outbuf);
89 ab9a70b2 2017-11-06 stsp inflateEnd(&zb->z);
92 ab9a70b2 2017-11-06 stsp static const struct got_error *
93 ab9a70b2 2017-11-06 stsp inflate_init(struct got_zstream_buf *zb, size_t bufsize)
95 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
97 ab9a70b2 2017-11-06 stsp memset(zb, 0, sizeof(*zb));
99 ab9a70b2 2017-11-06 stsp zb->z.zalloc = Z_NULL;
100 ab9a70b2 2017-11-06 stsp zb->z.zfree = Z_NULL;
101 ab9a70b2 2017-11-06 stsp if (inflateInit(&zb->z) != Z_OK) {
102 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_IO);
106 ab9a70b2 2017-11-06 stsp zb->inlen = zb->outlen = bufsize;
108 ab9a70b2 2017-11-06 stsp zb->inbuf = calloc(1, zb->inlen);
109 ab9a70b2 2017-11-06 stsp if (zb->inbuf == NULL) {
110 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
114 ab9a70b2 2017-11-06 stsp zb->outbuf = calloc(1, zb->outlen);
115 ab9a70b2 2017-11-06 stsp if (zb->outbuf == NULL) {
116 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
122 ab9a70b2 2017-11-06 stsp inflate_end(zb);
123 ab9a70b2 2017-11-06 stsp return err;
126 ab9a70b2 2017-11-06 stsp static const struct got_error *
127 ab9a70b2 2017-11-06 stsp inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
129 ab9a70b2 2017-11-06 stsp size_t last_total_out = zb->z.total_out;
130 ab9a70b2 2017-11-06 stsp z_stream *z = &zb->z;
131 ab9a70b2 2017-11-06 stsp int n, ret;
133 ab9a70b2 2017-11-06 stsp z->next_out = zb->outbuf;
134 ab9a70b2 2017-11-06 stsp z->avail_out = zb->outlen;
137 a3e2cbea 2017-12-01 stsp if (z->avail_in == 0) {
139 a3e2cbea 2017-12-01 stsp n = fread(zb->inbuf, 1, zb->inlen, f);
140 a3e2cbea 2017-12-01 stsp if (n == 0) {
141 a3e2cbea 2017-12-01 stsp if (ferror(f))
142 6c00b545 2018-01-17 stsp return got_ferror(f, GOT_ERR_IO);
143 a3e2cbea 2017-12-01 stsp *outlenp = 0;
144 a3e2cbea 2017-12-01 stsp return NULL;
146 a3e2cbea 2017-12-01 stsp z->next_in = zb->inbuf;
147 a3e2cbea 2017-12-01 stsp z->avail_in = n;
149 a3e2cbea 2017-12-01 stsp ret = inflate(z, Z_SYNC_FLUSH);
150 a3e2cbea 2017-12-01 stsp } while (ret == Z_OK && z->avail_out > 0);
152 a3e2cbea 2017-12-01 stsp if (ret != Z_OK) {
153 a3e2cbea 2017-12-01 stsp if (ret != Z_STREAM_END)
154 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_DECOMPRESSION);
155 a3e2cbea 2017-12-01 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
158 ab9a70b2 2017-11-06 stsp *outlenp = z->total_out - last_total_out;
159 ab9a70b2 2017-11-06 stsp return NULL;
162 ab9a70b2 2017-11-06 stsp static const struct got_error *
163 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
165 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
166 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
167 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
168 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
170 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
171 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
172 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
173 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
175 ab9a70b2 2017-11-06 stsp int type = 0;
176 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
178 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
180 ab9a70b2 2017-11-06 stsp if (p == NULL)
181 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
183 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
185 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
186 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
187 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
188 ab9a70b2 2017-11-06 stsp const char *errstr;
190 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
193 ab9a70b2 2017-11-06 stsp type = obj_types[i];
194 63323519 2017-11-06 stsp if (len <= tlen)
195 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
196 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
197 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
198 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
202 ab9a70b2 2017-11-06 stsp if (type == 0)
203 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
205 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
206 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
207 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
208 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
209 ab9a70b2 2017-11-06 stsp return NULL;
212 ab9a70b2 2017-11-06 stsp static const struct got_error *
213 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
216 ab9a70b2 2017-11-06 stsp const struct got_error *err;
217 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
219 744d9326 2017-12-01 stsp size_t len;
220 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
221 744d9326 2017-12-01 stsp size_t outlen, totlen;
222 ab9a70b2 2017-11-06 stsp int i, ret;
224 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
225 a3e2cbea 2017-12-01 stsp if (buf == NULL)
226 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_NO_MEM);
228 a3e2cbea 2017-12-01 stsp err = inflate_init(&zb, zbsize);
230 ab9a70b2 2017-11-06 stsp return err;
233 744d9326 2017-12-01 stsp totlen = 0;
235 a3e2cbea 2017-12-01 stsp err = inflate_read(&zb, f, &outlen);
238 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
239 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
240 e302c59e 2017-12-01 stsp if (buf == NULL) {
241 e302c59e 2017-12-01 stsp err = got_error(GOT_ERR_NO_MEM);
245 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
246 744d9326 2017-12-01 stsp totlen += outlen;
248 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
250 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
252 ab9a70b2 2017-11-06 stsp inflate_end(&zb);
253 ab9a70b2 2017-11-06 stsp return err;
256 d1cda826 2017-11-06 stsp static const struct got_error *
257 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
259 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
260 ab9a70b2 2017-11-06 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
261 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
263 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
264 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
266 ab9a70b2 2017-11-06 stsp got_object_id_str(id, hex, sizeof(hex));
268 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
269 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
270 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
272 d1cda826 2017-11-06 stsp free(path_objects);
273 d1cda826 2017-11-06 stsp return err;
276 4ee4114f 2018-01-23 stsp static const struct got_error *
277 4ee4114f 2018-01-23 stsp fopen_object(FILE **f, struct got_object *obj, struct got_repository *repo)
279 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
280 a1fd68d8 2018-01-12 stsp char *path;
282 3ee5fc21 2018-01-17 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
283 3ee5fc21 2018-01-17 stsp return got_packfile_extract_object(f, obj, repo);
285 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
287 d1cda826 2017-11-06 stsp return err;
288 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
289 4558fcd4 2018-01-14 stsp if (*f == NULL) {
290 6c00b545 2018-01-17 stsp err = got_error_from_errno();
294 4558fcd4 2018-01-14 stsp free(path);
295 4558fcd4 2018-01-14 stsp return err;
298 4558fcd4 2018-01-14 stsp const struct got_error *
299 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
300 4558fcd4 2018-01-14 stsp struct got_object_id *id)
302 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
303 6c00b545 2018-01-17 stsp char *path;
306 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
308 4558fcd4 2018-01-14 stsp return err;
310 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
311 6c00b545 2018-01-17 stsp if (f == NULL) {
312 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
313 6c00b545 2018-01-17 stsp err = got_error_from_errno();
316 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
319 6c00b545 2018-01-17 stsp if (*obj == NULL)
320 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
322 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
325 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
328 6c00b545 2018-01-17 stsp free(path);
329 6c00b545 2018-01-17 stsp if (err && f)
331 ab9a70b2 2017-11-06 stsp return err;
336 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
338 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
339 c3703302 2018-01-23 stsp struct got_delta *delta;
340 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
341 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
342 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
343 c3703302 2018-01-23 stsp got_delta_close(delta);
346 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
347 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
352 d1cda826 2017-11-06 stsp commit_object_valid(struct got_commit_object *commit)
357 d1cda826 2017-11-06 stsp if (commit == NULL)
361 d1cda826 2017-11-06 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
362 d1cda826 2017-11-06 stsp if (commit->tree_id.sha1[i] == 0)
365 d1cda826 2017-11-06 stsp if (n == SHA1_DIGEST_LENGTH)
371 d1cda826 2017-11-06 stsp static const struct got_error *
372 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
374 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
375 d1cda826 2017-11-06 stsp char *s = buf;
376 d1cda826 2017-11-06 stsp size_t tlen;
377 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
379 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
380 d1cda826 2017-11-06 stsp if (*commit == NULL)
381 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
383 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
385 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
386 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
387 d1cda826 2017-11-06 stsp remain -= tlen;
388 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
389 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
393 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
394 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
397 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
398 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
400 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
404 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
405 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
406 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
408 d1cda826 2017-11-06 stsp remain -= tlen;
409 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
410 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
414 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
415 d1cda826 2017-11-06 stsp if (pid == NULL) {
416 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
420 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(pid->id.sha1, s)) {
421 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
424 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
425 d1cda826 2017-11-06 stsp (*commit)->nparents++;
427 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
430 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
431 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
434 d1cda826 2017-11-06 stsp remain -= tlen;
435 d1cda826 2017-11-06 stsp if (remain <= 0) {
436 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
440 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
441 d1cda826 2017-11-06 stsp if (p == NULL) {
442 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
446 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
447 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
448 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
451 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
454 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
455 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
458 d1cda826 2017-11-06 stsp remain -= tlen;
459 d1cda826 2017-11-06 stsp if (remain <= 0) {
460 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
464 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
465 d1cda826 2017-11-06 stsp if (p == NULL) {
466 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
471 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
472 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
475 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
478 d1cda826 2017-11-06 stsp (*commit)->logmsg = strdup(s);
481 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
482 0ffeb3c2 2017-11-26 stsp return err;
485 0ffeb3c2 2017-11-26 stsp static void
486 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
488 0ffeb3c2 2017-11-26 stsp free(te->name);
492 0ffeb3c2 2017-11-26 stsp static const struct got_error *
493 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
494 0ffeb3c2 2017-11-26 stsp size_t maxlen)
496 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
497 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
498 0ffeb3c2 2017-11-26 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
500 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
501 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
502 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
504 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
505 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
507 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
510 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
511 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
513 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
515 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
516 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
517 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
520 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
521 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
525 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
526 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
527 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
531 0ffeb3c2 2017-11-26 stsp memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
532 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
535 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
536 d1cda826 2017-11-06 stsp return err;
539 d1cda826 2017-11-06 stsp static const struct got_error *
540 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
541 0ffeb3c2 2017-11-26 stsp char *buf, size_t len)
543 0ffeb3c2 2017-11-26 stsp size_t remain = len;
544 0ffeb3c2 2017-11-26 stsp int nentries;
546 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
547 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
548 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
550 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
552 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
553 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
554 0ffeb3c2 2017-11-26 stsp size_t elen;
556 0ffeb3c2 2017-11-26 stsp parse_tree_entry(&te, &elen, buf, remain);
557 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
558 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
559 0ffeb3c2 2017-11-26 stsp buf += elen;
560 0ffeb3c2 2017-11-26 stsp remain -= elen;
563 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
564 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
565 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
568 0ffeb3c2 2017-11-26 stsp return NULL;
571 0ffeb3c2 2017-11-26 stsp static const struct got_error *
572 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
573 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
575 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
576 d1cda826 2017-11-06 stsp struct got_zstream_buf zb;
577 d1cda826 2017-11-06 stsp size_t len;
579 d1cda826 2017-11-06 stsp int i, ret;
581 d1cda826 2017-11-06 stsp err = inflate_init(&zb, 8192);
583 d1cda826 2017-11-06 stsp return err;
586 d1cda826 2017-11-06 stsp err = inflate_read(&zb, f, &len);
587 d1cda826 2017-11-06 stsp if (err || len == 0)
589 d1cda826 2017-11-06 stsp } while (len < obj->hdrlen + obj->size);
591 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
592 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
596 d1cda826 2017-11-06 stsp /* Skip object header. */
597 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
598 d1cda826 2017-11-06 stsp err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
600 d1cda826 2017-11-06 stsp inflate_end(&zb);
601 d1cda826 2017-11-06 stsp return err;
604 d1cda826 2017-11-06 stsp const struct got_error *
605 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
606 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
608 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
611 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
612 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
614 4ee4114f 2018-01-23 stsp err = fopen_object(&f, obj, repo);
616 d1cda826 2017-11-06 stsp return err;
618 4558fcd4 2018-01-14 stsp err = read_commit_object(commit, repo, obj, f);
620 d1cda826 2017-11-06 stsp return err;
624 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
626 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
628 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
629 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
630 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
634 d1cda826 2017-11-06 stsp free(commit->author);
635 d1cda826 2017-11-06 stsp free(commit->committer);
636 d1cda826 2017-11-06 stsp free(commit->logmsg);
637 d1cda826 2017-11-06 stsp free(commit);
640 0ffeb3c2 2017-11-26 stsp static const struct got_error *
641 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
642 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
644 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
645 0ffeb3c2 2017-11-26 stsp struct got_zstream_buf zb;
646 0ffeb3c2 2017-11-26 stsp size_t len;
648 0ffeb3c2 2017-11-26 stsp int i, ret;
650 0ffeb3c2 2017-11-26 stsp err = inflate_init(&zb, 8192);
652 0ffeb3c2 2017-11-26 stsp return err;
655 0ffeb3c2 2017-11-26 stsp err = inflate_read(&zb, f, &len);
656 0ffeb3c2 2017-11-26 stsp if (err || len == 0)
658 0ffeb3c2 2017-11-26 stsp } while (len < obj->hdrlen + obj->size);
660 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
661 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
665 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
666 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
667 0ffeb3c2 2017-11-26 stsp err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
669 0ffeb3c2 2017-11-26 stsp inflate_end(&zb);
670 0ffeb3c2 2017-11-26 stsp return err;
673 0ffeb3c2 2017-11-26 stsp const struct got_error *
674 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
675 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
677 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
680 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
681 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
683 4ee4114f 2018-01-23 stsp err = fopen_object(&f, obj, repo);
685 0ffeb3c2 2017-11-26 stsp return err;
687 4558fcd4 2018-01-14 stsp err = read_tree_object(tree, repo, obj, f);
689 0ffeb3c2 2017-11-26 stsp return err;
693 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
695 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
697 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
698 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
699 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
700 f715ca7f 2017-11-27 stsp tree_entry_close(te);
703 f715ca7f 2017-11-27 stsp free(tree);
706 68482ea3 2017-11-27 stsp const struct got_error *
707 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
708 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
710 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
712 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
713 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
715 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
716 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
718 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
719 4558fcd4 2018-01-14 stsp if (*blob == NULL)
720 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_NO_MEM);
722 4ee4114f 2018-01-23 stsp err = fopen_object(&((*blob)->f), obj, repo);
724 68482ea3 2017-11-27 stsp free(*blob);
725 4558fcd4 2018-01-14 stsp return err;
728 68482ea3 2017-11-27 stsp err = inflate_init(&(*blob)->zb, blocksize);
729 68482ea3 2017-11-27 stsp if (err != NULL) {
730 68482ea3 2017-11-27 stsp fclose((*blob)->f);
731 68482ea3 2017-11-27 stsp free(*blob);
732 68482ea3 2017-11-27 stsp return err;
735 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
736 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
738 68482ea3 2017-11-27 stsp return err;
742 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
744 68482ea3 2017-11-27 stsp inflate_end(&blob->zb);
745 68482ea3 2017-11-27 stsp fclose(blob->f);
746 68482ea3 2017-11-27 stsp free(blob);
749 68482ea3 2017-11-27 stsp const struct got_error *
750 68482ea3 2017-11-27 stsp got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
752 68482ea3 2017-11-27 stsp return inflate_read(&blob->zb, blob->f, outlenp);