2 d71d75ad 2017-11-05 stsp * Copyright (c) 2017 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 d71d75ad 2017-11-05 stsp #include <stdio.h>
21 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
22 ab9a70b2 2017-11-06 stsp #include <string.h>
23 d71d75ad 2017-11-05 stsp #include <sha1.h>
24 ab9a70b2 2017-11-06 stsp #include <zlib.h>
25 ab9a70b2 2017-11-06 stsp #include <ctype.h>
26 ab9a70b2 2017-11-06 stsp #include <limits.h>
28 ab9a70b2 2017-11-06 stsp #include "got_error.h"
29 d71d75ad 2017-11-05 stsp #include "got_object.h"
30 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
31 d1cda826 2017-11-06 stsp #include "got_sha1.h"
34 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
37 ab9a70b2 2017-11-06 stsp #ifndef nitems
38 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
41 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
42 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
43 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
45 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
46 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
47 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
48 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
51 d71d75ad 2017-11-05 stsp got_object_id_str(struct got_object_id *id, char *buf, size_t size)
53 d71d75ad 2017-11-05 stsp char *p = buf;
54 d71d75ad 2017-11-05 stsp char hex[3];
57 d71d75ad 2017-11-05 stsp if (size < SHA1_DIGEST_STRING_LENGTH)
58 d71d75ad 2017-11-05 stsp return NULL;
60 d71d75ad 2017-11-05 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
61 d71d75ad 2017-11-05 stsp snprintf(hex, sizeof(hex), "%.2x", id->sha1[i]);
62 d71d75ad 2017-11-05 stsp p[0] = hex[0];
63 d71d75ad 2017-11-05 stsp p[1] = hex[1];
66 d71d75ad 2017-11-05 stsp p[0] = '\0';
72 ab9a70b2 2017-11-06 stsp inflate_end(struct got_zstream_buf *zb)
74 ab9a70b2 2017-11-06 stsp free(zb->inbuf);
75 ab9a70b2 2017-11-06 stsp free(zb->outbuf);
76 ab9a70b2 2017-11-06 stsp inflateEnd(&zb->z);
79 ab9a70b2 2017-11-06 stsp static const struct got_error *
80 ab9a70b2 2017-11-06 stsp inflate_init(struct got_zstream_buf *zb, size_t bufsize)
82 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
84 ab9a70b2 2017-11-06 stsp memset(zb, 0, sizeof(*zb));
86 ab9a70b2 2017-11-06 stsp zb->z.zalloc = Z_NULL;
87 ab9a70b2 2017-11-06 stsp zb->z.zfree = Z_NULL;
88 ab9a70b2 2017-11-06 stsp if (inflateInit(&zb->z) != Z_OK) {
89 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_IO);
93 ab9a70b2 2017-11-06 stsp zb->inlen = zb->outlen = bufsize;
95 ab9a70b2 2017-11-06 stsp zb->inbuf = calloc(1, zb->inlen);
96 ab9a70b2 2017-11-06 stsp if (zb->inbuf == NULL) {
97 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
101 ab9a70b2 2017-11-06 stsp zb->outbuf = calloc(1, zb->outlen);
102 ab9a70b2 2017-11-06 stsp if (zb->outbuf == NULL) {
103 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
109 ab9a70b2 2017-11-06 stsp inflate_end(zb);
110 ab9a70b2 2017-11-06 stsp return err;
113 ab9a70b2 2017-11-06 stsp static const struct got_error *
114 ab9a70b2 2017-11-06 stsp inflate_read(struct got_zstream_buf *zb, FILE *f, size_t *outlenp)
116 ab9a70b2 2017-11-06 stsp size_t last_total_out = zb->z.total_out;
117 ab9a70b2 2017-11-06 stsp z_stream *z = &zb->z;
118 ab9a70b2 2017-11-06 stsp int n, ret;
120 ab9a70b2 2017-11-06 stsp z->next_out = zb->outbuf;
121 ab9a70b2 2017-11-06 stsp z->avail_out = zb->outlen;
124 a3e2cbea 2017-12-01 stsp if (z->avail_in == 0) {
126 a3e2cbea 2017-12-01 stsp n = fread(zb->inbuf, 1, zb->inlen, f);
127 a3e2cbea 2017-12-01 stsp if (n == 0) {
128 a3e2cbea 2017-12-01 stsp if (ferror(f))
129 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_IO);
130 a3e2cbea 2017-12-01 stsp *outlenp = 0;
131 a3e2cbea 2017-12-01 stsp return NULL;
133 a3e2cbea 2017-12-01 stsp z->next_in = zb->inbuf;
134 a3e2cbea 2017-12-01 stsp z->avail_in = n;
136 a3e2cbea 2017-12-01 stsp ret = inflate(z, Z_SYNC_FLUSH);
137 a3e2cbea 2017-12-01 stsp } while (ret == Z_OK && z->avail_out > 0);
139 a3e2cbea 2017-12-01 stsp if (ret != Z_OK) {
140 a3e2cbea 2017-12-01 stsp if (ret != Z_STREAM_END)
141 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_DECOMPRESSION);
142 a3e2cbea 2017-12-01 stsp zb->flags |= GOT_ZSTREAM_F_HAVE_MORE;
145 ab9a70b2 2017-11-06 stsp *outlenp = z->total_out - last_total_out;
146 ab9a70b2 2017-11-06 stsp return NULL;
149 ab9a70b2 2017-11-06 stsp static const struct got_error *
150 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
152 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
153 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
154 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
155 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
157 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
158 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
159 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
160 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
162 ab9a70b2 2017-11-06 stsp int type = 0;
163 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
165 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
167 ab9a70b2 2017-11-06 stsp if (p == NULL)
168 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
170 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
172 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
173 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
174 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
175 ab9a70b2 2017-11-06 stsp const char *errstr;
177 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
180 ab9a70b2 2017-11-06 stsp type = obj_types[i];
181 63323519 2017-11-06 stsp if (len <= tlen)
182 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
183 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
184 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
185 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
189 ab9a70b2 2017-11-06 stsp if (type == 0)
190 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
192 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
193 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
194 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
195 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
196 ab9a70b2 2017-11-06 stsp return NULL;
199 ab9a70b2 2017-11-06 stsp static const struct got_error *
200 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
201 ab9a70b2 2017-11-06 stsp const char *path)
203 ab9a70b2 2017-11-06 stsp const struct got_error *err;
205 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
207 744d9326 2017-12-01 stsp size_t len;
208 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
209 744d9326 2017-12-01 stsp size_t outlen, totlen;
210 ab9a70b2 2017-11-06 stsp int i, ret;
212 ab9a70b2 2017-11-06 stsp f = fopen(path, "rb");
213 ab9a70b2 2017-11-06 stsp if (f == NULL)
214 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_PATH);
216 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
217 a3e2cbea 2017-12-01 stsp if (buf == NULL)
218 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_NO_MEM);
220 a3e2cbea 2017-12-01 stsp err = inflate_init(&zb, zbsize);
223 ab9a70b2 2017-11-06 stsp return err;
227 744d9326 2017-12-01 stsp totlen = 0;
229 a3e2cbea 2017-12-01 stsp err = inflate_read(&zb, f, &outlen);
232 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
233 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
234 e302c59e 2017-12-01 stsp if (buf == NULL) {
235 e302c59e 2017-12-01 stsp err = got_error(GOT_ERR_NO_MEM);
238 6cb5fd0d 2017-12-01 stsp buf += totlen;
240 744d9326 2017-12-01 stsp memcpy(buf, zb.outbuf, outlen);
241 744d9326 2017-12-01 stsp totlen += outlen;
243 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
245 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
247 ab9a70b2 2017-11-06 stsp inflate_end(&zb);
249 ab9a70b2 2017-11-06 stsp return err;
252 d1cda826 2017-11-06 stsp static const struct got_error *
253 d1cda826 2017-11-06 stsp object_path(char **path, struct got_object_id *id,
254 d1cda826 2017-11-06 stsp struct got_repository *repo)
256 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
257 ab9a70b2 2017-11-06 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
258 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
260 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
261 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
263 ab9a70b2 2017-11-06 stsp got_object_id_str(id, hex, sizeof(hex));
265 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
266 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
267 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
269 d1cda826 2017-11-06 stsp free(path_objects);
270 d1cda826 2017-11-06 stsp return err;
273 d1cda826 2017-11-06 stsp const struct got_error *
274 d1cda826 2017-11-06 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
275 d1cda826 2017-11-06 stsp struct got_object_id *id)
277 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
278 d1cda826 2017-11-06 stsp char *path = NULL;
280 d1cda826 2017-11-06 stsp err = object_path(&path, id, repo);
282 d1cda826 2017-11-06 stsp return err;
284 ab9a70b2 2017-11-06 stsp err = read_object_header(obj, repo, path);
285 ab9a70b2 2017-11-06 stsp if (err == NULL)
286 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
288 ab9a70b2 2017-11-06 stsp free(path);
289 ab9a70b2 2017-11-06 stsp return err;
293 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
299 d1cda826 2017-11-06 stsp commit_object_valid(struct got_commit_object *commit)
304 d1cda826 2017-11-06 stsp if (commit == NULL)
308 d1cda826 2017-11-06 stsp for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
309 d1cda826 2017-11-06 stsp if (commit->tree_id.sha1[i] == 0)
312 d1cda826 2017-11-06 stsp if (n == SHA1_DIGEST_LENGTH)
318 d1cda826 2017-11-06 stsp static const struct got_error *
319 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
321 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
322 d1cda826 2017-11-06 stsp char *s = buf;
323 d1cda826 2017-11-06 stsp size_t tlen;
324 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
326 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
327 d1cda826 2017-11-06 stsp if (*commit == NULL)
328 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
330 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
332 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
333 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
334 d1cda826 2017-11-06 stsp remain -= tlen;
335 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
336 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
340 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
341 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
344 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
345 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
347 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
352 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
353 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
355 d1cda826 2017-11-06 stsp remain -= tlen;
356 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
357 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
361 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
362 d1cda826 2017-11-06 stsp if (pid == NULL) {
363 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
367 d1cda826 2017-11-06 stsp if (!got_parse_sha1_digest(pid->id.sha1, s)) {
368 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
371 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
372 d1cda826 2017-11-06 stsp (*commit)->nparents++;
374 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
377 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
378 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
381 d1cda826 2017-11-06 stsp remain -= tlen;
382 d1cda826 2017-11-06 stsp if (remain <= 0) {
383 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
387 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
388 d1cda826 2017-11-06 stsp if (p == NULL) {
389 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
393 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
394 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
395 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
398 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
401 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
402 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
405 d1cda826 2017-11-06 stsp remain -= tlen;
406 d1cda826 2017-11-06 stsp if (remain <= 0) {
407 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
411 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
412 d1cda826 2017-11-06 stsp if (p == NULL) {
413 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
417 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
418 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
419 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
422 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
425 d1cda826 2017-11-06 stsp (*commit)->logmsg = strdup(s);
428 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
429 0ffeb3c2 2017-11-26 stsp return err;
432 0ffeb3c2 2017-11-26 stsp static void
433 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
435 0ffeb3c2 2017-11-26 stsp free(te->name);
439 0ffeb3c2 2017-11-26 stsp static const struct got_error *
440 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
441 0ffeb3c2 2017-11-26 stsp size_t maxlen)
443 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
444 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
445 0ffeb3c2 2017-11-26 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
447 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
448 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
449 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
451 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
452 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
454 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
457 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
458 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
460 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
462 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
463 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
464 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
468 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
472 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
473 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
474 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
477 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
478 0ffeb3c2 2017-11-26 stsp memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
479 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
482 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
483 d1cda826 2017-11-06 stsp return err;
486 d1cda826 2017-11-06 stsp static const struct got_error *
487 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
488 0ffeb3c2 2017-11-26 stsp char *buf, size_t len)
490 0ffeb3c2 2017-11-26 stsp size_t remain = len;
491 0ffeb3c2 2017-11-26 stsp int nentries;
493 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
494 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
495 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
497 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
499 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
500 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
501 0ffeb3c2 2017-11-26 stsp size_t elen;
503 0ffeb3c2 2017-11-26 stsp parse_tree_entry(&te, &elen, buf, remain);
504 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
505 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
506 0ffeb3c2 2017-11-26 stsp buf += elen;
507 0ffeb3c2 2017-11-26 stsp remain -= elen;
510 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
511 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
512 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
515 0ffeb3c2 2017-11-26 stsp return NULL;
518 0ffeb3c2 2017-11-26 stsp static const struct got_error *
519 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
520 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj, const char *path)
522 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
524 d1cda826 2017-11-06 stsp struct got_zstream_buf zb;
525 d1cda826 2017-11-06 stsp size_t len;
527 d1cda826 2017-11-06 stsp int i, ret;
529 d1cda826 2017-11-06 stsp f = fopen(path, "rb");
530 d1cda826 2017-11-06 stsp if (f == NULL)
531 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_BAD_PATH);
533 d1cda826 2017-11-06 stsp err = inflate_init(&zb, 8192);
536 d1cda826 2017-11-06 stsp return err;
540 d1cda826 2017-11-06 stsp err = inflate_read(&zb, f, &len);
541 d1cda826 2017-11-06 stsp if (err || len == 0)
543 d1cda826 2017-11-06 stsp } while (len < obj->hdrlen + obj->size);
545 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
546 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
550 d1cda826 2017-11-06 stsp /* Skip object header. */
551 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
552 d1cda826 2017-11-06 stsp err = parse_commit_object(commit, zb.outbuf + obj->hdrlen, len);
554 d1cda826 2017-11-06 stsp inflate_end(&zb);
556 d1cda826 2017-11-06 stsp return err;
559 d1cda826 2017-11-06 stsp const struct got_error *
560 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
561 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
563 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
564 d1cda826 2017-11-06 stsp char *path = NULL;
566 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
567 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
569 d1cda826 2017-11-06 stsp err = object_path(&path, &obj->id, repo);
571 d1cda826 2017-11-06 stsp return err;
573 d1cda826 2017-11-06 stsp err = read_commit_object(commit, repo, obj, path);
574 d1cda826 2017-11-06 stsp free(path);
575 d1cda826 2017-11-06 stsp return err;
579 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
581 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
583 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
584 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
585 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
589 d1cda826 2017-11-06 stsp free(commit->author);
590 d1cda826 2017-11-06 stsp free(commit->committer);
591 d1cda826 2017-11-06 stsp free(commit->logmsg);
592 d1cda826 2017-11-06 stsp free(commit);
595 0ffeb3c2 2017-11-26 stsp static const struct got_error *
596 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
597 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj, const char *path)
599 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
601 0ffeb3c2 2017-11-26 stsp struct got_zstream_buf zb;
602 0ffeb3c2 2017-11-26 stsp size_t len;
604 0ffeb3c2 2017-11-26 stsp int i, ret;
606 0ffeb3c2 2017-11-26 stsp f = fopen(path, "rb");
607 0ffeb3c2 2017-11-26 stsp if (f == NULL)
608 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_PATH);
610 0ffeb3c2 2017-11-26 stsp err = inflate_init(&zb, 8192);
613 0ffeb3c2 2017-11-26 stsp return err;
617 0ffeb3c2 2017-11-26 stsp err = inflate_read(&zb, f, &len);
618 0ffeb3c2 2017-11-26 stsp if (err || len == 0)
620 0ffeb3c2 2017-11-26 stsp } while (len < obj->hdrlen + obj->size);
622 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
623 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
628 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
629 0ffeb3c2 2017-11-26 stsp err = parse_tree_object(tree, repo, zb.outbuf + obj->hdrlen, len);
631 0ffeb3c2 2017-11-26 stsp inflate_end(&zb);
633 0ffeb3c2 2017-11-26 stsp return err;
636 0ffeb3c2 2017-11-26 stsp const struct got_error *
637 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
638 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
640 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
641 0ffeb3c2 2017-11-26 stsp char *path = NULL;
643 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
644 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
646 0ffeb3c2 2017-11-26 stsp err = object_path(&path, &obj->id, repo);
648 0ffeb3c2 2017-11-26 stsp return err;
650 0ffeb3c2 2017-11-26 stsp err = read_tree_object(tree, repo, obj, path);
651 0ffeb3c2 2017-11-26 stsp free(path);
652 0ffeb3c2 2017-11-26 stsp return err;
656 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
658 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
660 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
661 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
662 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
663 f715ca7f 2017-11-27 stsp tree_entry_close(te);
666 f715ca7f 2017-11-27 stsp free(tree);
669 68482ea3 2017-11-27 stsp const struct got_error *
670 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
671 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
673 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
674 68482ea3 2017-11-27 stsp char *path;
676 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
677 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
679 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
680 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
682 68482ea3 2017-11-27 stsp err = object_path(&path, &obj->id, repo);
684 68482ea3 2017-11-27 stsp return err;
686 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
687 68482ea3 2017-11-27 stsp if (*blob == NULL) {
688 68482ea3 2017-11-27 stsp free(path);
689 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_NO_MEM);
692 68482ea3 2017-11-27 stsp (*blob)->f = fopen(path, "rb");
693 68482ea3 2017-11-27 stsp if ((*blob)->f == NULL) {
694 68482ea3 2017-11-27 stsp free(*blob);
695 68482ea3 2017-11-27 stsp free(path);
696 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_BAD_PATH);
699 68482ea3 2017-11-27 stsp err = inflate_init(&(*blob)->zb, blocksize);
700 68482ea3 2017-11-27 stsp if (err != NULL) {
701 68482ea3 2017-11-27 stsp fclose((*blob)->f);
702 68482ea3 2017-11-27 stsp free(*blob);
703 68482ea3 2017-11-27 stsp free(path);
704 68482ea3 2017-11-27 stsp return err;
707 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
708 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
710 68482ea3 2017-11-27 stsp free(path);
711 68482ea3 2017-11-27 stsp return err;
715 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
717 68482ea3 2017-11-27 stsp inflate_end(&blob->zb);
718 68482ea3 2017-11-27 stsp fclose(blob->f);
719 68482ea3 2017-11-27 stsp free(blob);
722 68482ea3 2017-11-27 stsp const struct got_error *
723 68482ea3 2017-11-27 stsp got_object_blob_read_block(struct got_blob_object *blob, size_t *outlenp)
725 68482ea3 2017-11-27 stsp return inflate_read(&blob->zb, blob->f, outlenp);