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"
33 32cb896c 2018-03-11 stsp #include "got_sha1_lib.h"
34 32cb896c 2018-03-11 stsp #include "got_delta_lib.h"
35 32cb896c 2018-03-11 stsp #include "got_pack_lib.h"
36 32cb896c 2018-03-11 stsp #include "got_zbuf_lib.h"
37 32cb896c 2018-03-11 stsp #include "got_object_lib.h"
40 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
43 ab9a70b2 2017-11-06 stsp #ifndef nitems
44 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
48 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
49 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
51 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
52 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
53 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
54 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
56 ef0981d5 2018-02-12 stsp const struct got_error *
57 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
59 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
61 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
62 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
63 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_NO_MEM);
65 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
66 ef0981d5 2018-02-12 stsp free(*outbuf);
67 ef0981d5 2018-02-12 stsp *outbuf = NULL;
68 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
71 ef0981d5 2018-02-12 stsp return NULL;
75 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
77 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
81 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
83 b107e67f 2018-01-19 stsp switch (obj->type) {
84 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
87 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
88 b107e67f 2018-01-19 stsp return obj->type;
94 96f5e8b3 2018-01-23 stsp /* not reached */
98 ab9a70b2 2017-11-06 stsp static const struct got_error *
99 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
101 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
102 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
103 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
104 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
106 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
107 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
108 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
109 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
111 ab9a70b2 2017-11-06 stsp int type = 0;
112 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
114 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
116 ab9a70b2 2017-11-06 stsp if (p == NULL)
117 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
119 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
121 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
122 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
123 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
124 ab9a70b2 2017-11-06 stsp const char *errstr;
126 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
129 ab9a70b2 2017-11-06 stsp type = obj_types[i];
130 63323519 2017-11-06 stsp if (len <= tlen)
131 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
132 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
133 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
134 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
138 ab9a70b2 2017-11-06 stsp if (type == 0)
139 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
141 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
142 1db76ab5 2018-01-26 mpi if (*obj == NULL)
143 1db76ab5 2018-01-26 mpi return got_error(GOT_ERR_NO_MEM);
144 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
145 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
146 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
147 ab9a70b2 2017-11-06 stsp return NULL;
150 ab9a70b2 2017-11-06 stsp static const struct got_error *
151 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
154 ab9a70b2 2017-11-06 stsp const struct got_error *err;
155 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
157 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
158 744d9326 2017-12-01 stsp size_t outlen, totlen;
161 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
162 a3e2cbea 2017-12-01 stsp if (buf == NULL)
163 a3e2cbea 2017-12-01 stsp return got_error(GOT_ERR_NO_MEM);
165 4ca7b755 2018-01-26 stsp err = got_inflate_init(&zb, zbsize);
167 ab9a70b2 2017-11-06 stsp return err;
170 744d9326 2017-12-01 stsp totlen = 0;
172 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
175 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
176 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
177 e302c59e 2017-12-01 stsp if (buf == NULL) {
178 e302c59e 2017-12-01 stsp err = got_error(GOT_ERR_NO_MEM);
182 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
183 744d9326 2017-12-01 stsp totlen += outlen;
185 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
187 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
189 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
190 ab9a70b2 2017-11-06 stsp return err;
193 d1cda826 2017-11-06 stsp static const struct got_error *
194 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
196 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
198 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
200 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
201 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
203 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
205 ef0981d5 2018-02-12 stsp return err;
207 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
208 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
209 ab9a70b2 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
212 d1cda826 2017-11-06 stsp free(path_objects);
213 d1cda826 2017-11-06 stsp return err;
216 4ee4114f 2018-01-23 stsp static const struct got_error *
217 eb651edf 2018-02-11 stsp open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
219 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
220 a1fd68d8 2018-01-12 stsp char *path;
222 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
224 d1cda826 2017-11-06 stsp return err;
225 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
226 4558fcd4 2018-01-14 stsp if (*f == NULL) {
227 6c00b545 2018-01-17 stsp err = got_error_from_errno();
231 4558fcd4 2018-01-14 stsp free(path);
232 4558fcd4 2018-01-14 stsp return err;
235 4558fcd4 2018-01-14 stsp const struct got_error *
236 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
237 4558fcd4 2018-01-14 stsp struct got_object_id *id)
239 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
240 6c00b545 2018-01-17 stsp char *path;
243 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
245 4558fcd4 2018-01-14 stsp return err;
247 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
248 6c00b545 2018-01-17 stsp if (f == NULL) {
249 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
250 6c00b545 2018-01-17 stsp err = got_error_from_errno();
253 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
256 6c00b545 2018-01-17 stsp if (*obj == NULL)
257 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
259 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
262 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
265 6c00b545 2018-01-17 stsp free(path);
266 6c00b545 2018-01-17 stsp if (err && f)
268 ab9a70b2 2017-11-06 stsp return err;
272 6dfa2fd3 2018-02-12 stsp const struct got_error *
273 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
274 6dfa2fd3 2018-02-12 stsp const char *id_str)
276 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
278 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
279 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
281 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
285 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
287 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
288 c3703302 2018-01-23 stsp struct got_delta *delta;
289 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
290 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
291 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
292 c3703302 2018-01-23 stsp got_delta_close(delta);
295 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
296 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
300 d1cda826 2017-11-06 stsp static const struct got_error *
301 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
303 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
304 d1cda826 2017-11-06 stsp char *s = buf;
305 d1cda826 2017-11-06 stsp size_t tlen;
306 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
308 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
309 d1cda826 2017-11-06 stsp if (*commit == NULL)
310 59ece79d 2018-02-12 stsp return got_error(GOT_ERR_NO_MEM);
311 59ece79d 2018-02-12 stsp (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
312 59ece79d 2018-02-12 stsp if ((*commit)->tree_id == NULL) {
313 59ece79d 2018-02-12 stsp free(*commit);
314 59ece79d 2018-02-12 stsp *commit = NULL;
315 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
318 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
320 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
321 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
322 d1cda826 2017-11-06 stsp remain -= tlen;
323 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
324 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
328 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
329 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
332 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
333 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
335 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
339 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
340 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
341 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
343 d1cda826 2017-11-06 stsp remain -= tlen;
344 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
345 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
350 d1cda826 2017-11-06 stsp if (pid == NULL) {
351 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
354 59ece79d 2018-02-12 stsp pid->id = calloc(1, sizeof(*pid->id));
355 59ece79d 2018-02-12 stsp if (pid->id == NULL) {
357 59ece79d 2018-02-12 stsp err = got_error(GOT_ERR_NO_MEM);
361 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest(pid->id->sha1, s)) {
362 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
363 59ece79d 2018-02-12 stsp free(pid->id);
367 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
368 d1cda826 2017-11-06 stsp (*commit)->nparents++;
370 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
371 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
374 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
375 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
378 d1cda826 2017-11-06 stsp remain -= tlen;
379 d1cda826 2017-11-06 stsp if (remain <= 0) {
380 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
384 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
385 d1cda826 2017-11-06 stsp if (p == NULL) {
386 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
390 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
391 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
392 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
395 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
396 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
399 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
400 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
403 d1cda826 2017-11-06 stsp remain -= tlen;
404 d1cda826 2017-11-06 stsp if (remain <= 0) {
405 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
409 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
410 d1cda826 2017-11-06 stsp if (p == NULL) {
411 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
415 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
416 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
417 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_NO_MEM);
420 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
421 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
424 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
425 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
426 eb651edf 2018-02-11 stsp err = got_error(GOT_ERR_NO_MEM);
431 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
432 59ece79d 2018-02-12 stsp *commit = NULL;
434 0ffeb3c2 2017-11-26 stsp return err;
437 0ffeb3c2 2017-11-26 stsp static void
438 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
440 59ece79d 2018-02-12 stsp free(te->id);
441 0ffeb3c2 2017-11-26 stsp free(te->name);
445 0ffeb3c2 2017-11-26 stsp static const struct got_error *
446 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
447 0ffeb3c2 2017-11-26 stsp size_t maxlen)
449 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
450 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
452 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
453 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
454 59ece79d 2018-02-12 stsp return got_error(GOT_ERR_NO_MEM);
456 59ece79d 2018-02-12 stsp (*te)->id = calloc(1, sizeof(*(*te)->id));
457 59ece79d 2018-02-12 stsp if ((*te)->id == NULL) {
459 59ece79d 2018-02-12 stsp *te = NULL;
460 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
463 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
464 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
466 59ece79d 2018-02-12 stsp *te = NULL;
467 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
470 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
471 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
473 59ece79d 2018-02-12 stsp *te = NULL;
474 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
476 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
477 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
478 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
481 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
482 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
486 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
487 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
488 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
492 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
493 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
496 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
497 59ece79d 2018-02-12 stsp *te = NULL;
499 d1cda826 2017-11-06 stsp return err;
502 d1cda826 2017-11-06 stsp static const struct got_error *
503 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
504 0ffeb3c2 2017-11-26 stsp char *buf, size_t len)
506 90356acc 2018-01-27 stsp const struct got_error *err;
507 0ffeb3c2 2017-11-26 stsp size_t remain = len;
509 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
510 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
511 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
513 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
515 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
516 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
517 0ffeb3c2 2017-11-26 stsp size_t elen;
519 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
521 90356acc 2018-01-27 stsp return err;
522 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
523 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
524 0ffeb3c2 2017-11-26 stsp buf += elen;
525 0ffeb3c2 2017-11-26 stsp remain -= elen;
528 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
529 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
530 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
533 0ffeb3c2 2017-11-26 stsp return NULL;
536 0ffeb3c2 2017-11-26 stsp static const struct got_error *
537 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
539 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
540 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
541 eb651edf 2018-02-11 stsp size_t n, total, remain;
542 eb651edf 2018-02-11 stsp uint8_t *buf;
544 eb651edf 2018-02-11 stsp *outbuf = NULL;
545 eb651edf 2018-02-11 stsp *outlen = 0;
547 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
548 eb651edf 2018-02-11 stsp if (buf == NULL)
549 eb651edf 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
551 eb651edf 2018-02-11 stsp remain = blocksize;
553 eb651edf 2018-02-11 stsp while (1) {
554 eb651edf 2018-02-11 stsp if (remain == 0) {
555 eb651edf 2018-02-11 stsp uint8_t *newbuf;
556 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
557 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
558 eb651edf 2018-02-11 stsp err = got_error(GOT_ERR_NO_MEM);
561 eb651edf 2018-02-11 stsp buf = newbuf;
562 eb651edf 2018-02-11 stsp remain += blocksize;
564 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
565 eb651edf 2018-02-11 stsp if (n == 0) {
566 eb651edf 2018-02-11 stsp if (ferror(f)) {
567 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
570 eb651edf 2018-02-11 stsp break; /* EOF */
572 eb651edf 2018-02-11 stsp remain -= n;
573 eb651edf 2018-02-11 stsp total += n;
577 eb651edf 2018-02-11 stsp if (err == NULL) {
578 eb651edf 2018-02-11 stsp *outbuf = buf;
579 eb651edf 2018-02-11 stsp *outlen = total;
582 eb651edf 2018-02-11 stsp return err;
585 eb651edf 2018-02-11 stsp static const struct got_error *
586 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
587 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
589 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
590 d1cda826 2017-11-06 stsp size_t len;
591 eb651edf 2018-02-11 stsp uint8_t *p;
593 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
594 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
596 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
598 d1cda826 2017-11-06 stsp return err;
600 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
601 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
605 d1cda826 2017-11-06 stsp /* Skip object header. */
606 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
607 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
610 d1cda826 2017-11-06 stsp return err;
613 d1cda826 2017-11-06 stsp const struct got_error *
614 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
615 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
617 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
620 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
621 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
623 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
624 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&f, obj, repo);
626 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
628 d1cda826 2017-11-06 stsp return err;
630 4558fcd4 2018-01-14 stsp err = read_commit_object(commit, repo, obj, f);
632 d1cda826 2017-11-06 stsp return err;
636 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
638 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
640 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
641 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
642 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
643 59ece79d 2018-02-12 stsp free(pid->id);
647 59ece79d 2018-02-12 stsp free(commit->tree_id);
648 d1cda826 2017-11-06 stsp free(commit->author);
649 d1cda826 2017-11-06 stsp free(commit->committer);
650 d1cda826 2017-11-06 stsp free(commit->logmsg);
651 d1cda826 2017-11-06 stsp free(commit);
654 0ffeb3c2 2017-11-26 stsp static const struct got_error *
655 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
656 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
658 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
659 0ffeb3c2 2017-11-26 stsp size_t len;
660 eb651edf 2018-02-11 stsp uint8_t *p;
662 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
663 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
665 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
667 0ffeb3c2 2017-11-26 stsp return err;
669 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
670 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
675 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
676 eb651edf 2018-02-11 stsp err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
679 0ffeb3c2 2017-11-26 stsp return err;
682 0ffeb3c2 2017-11-26 stsp const struct got_error *
683 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
684 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
686 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
689 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
690 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
692 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
693 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&f, obj, repo);
695 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
697 0ffeb3c2 2017-11-26 stsp return err;
699 4558fcd4 2018-01-14 stsp err = read_tree_object(tree, repo, obj, f);
701 0ffeb3c2 2017-11-26 stsp return err;
705 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
707 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
709 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
710 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
711 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
712 f715ca7f 2017-11-27 stsp tree_entry_close(te);
715 f715ca7f 2017-11-27 stsp free(tree);
718 68482ea3 2017-11-27 stsp const struct got_error *
719 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
720 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
722 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
724 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
725 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
727 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
728 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
730 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
731 4558fcd4 2018-01-14 stsp if (*blob == NULL)
732 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_NO_MEM);
734 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
735 eb651edf 2018-02-11 stsp (*blob)->read_buf = calloc(1, blocksize);
736 eb651edf 2018-02-11 stsp if ((*blob)->read_buf == NULL)
737 eb651edf 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
738 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
740 eb651edf 2018-02-11 stsp return err;
742 eb651edf 2018-02-11 stsp err = open_loose_object(&((*blob)->f), obj, repo);
744 eb651edf 2018-02-11 stsp free(*blob);
745 eb651edf 2018-02-11 stsp return err;
748 eb651edf 2018-02-11 stsp err = got_inflate_init(&(*blob)->zb, blocksize);
749 eb651edf 2018-02-11 stsp if (err != NULL) {
750 eb651edf 2018-02-11 stsp fclose((*blob)->f);
751 eb651edf 2018-02-11 stsp free(*blob);
752 eb651edf 2018-02-11 stsp return err;
755 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
756 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
759 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
760 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
761 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
763 68482ea3 2017-11-27 stsp return err;
767 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
769 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
770 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
772 eb651edf 2018-02-11 stsp free(blob->read_buf);
773 68482ea3 2017-11-27 stsp fclose(blob->f);
774 68482ea3 2017-11-27 stsp free(blob);
778 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
780 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
784 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
786 f934cf2c 2018-02-12 stsp return blob->hdrlen;
789 f934cf2c 2018-02-12 stsp const uint8_t *
790 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
792 f934cf2c 2018-02-12 stsp return blob->read_buf;
795 68482ea3 2017-11-27 stsp const struct got_error *
796 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
800 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
801 eb651edf 2018-02-11 stsp return got_inflate_read(&blob->zb, blob->f, outlenp);
803 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
804 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
805 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
806 eb651edf 2018-02-11 stsp *outlenp = n;
807 eb651edf 2018-02-11 stsp return NULL;