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 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
34 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
35 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
36 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
37 718b3ab0 2018-03-17 stsp #include "got_lib_object.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 0a585a0d 2018-03-17 stsp return got_error_from_errno();
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);
80 8bf5b3c9 2018-03-17 stsp struct got_object_id *
81 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
83 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
85 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
86 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
87 8bf5b3c9 2018-03-17 stsp return NULL;
88 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
92 3235492e 2018-04-01 stsp struct got_object_id *
93 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
95 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
99 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
101 b107e67f 2018-01-19 stsp switch (obj->type) {
102 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
103 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
104 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
105 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
106 b107e67f 2018-01-19 stsp return obj->type;
112 96f5e8b3 2018-01-23 stsp /* not reached */
116 ab9a70b2 2017-11-06 stsp static const struct got_error *
117 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
119 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
120 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
121 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
122 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
124 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
125 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
126 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
127 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
129 ab9a70b2 2017-11-06 stsp int type = 0;
130 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
132 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
134 ab9a70b2 2017-11-06 stsp if (p == NULL)
135 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
137 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
139 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
140 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
141 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
142 ab9a70b2 2017-11-06 stsp const char *errstr;
144 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
147 ab9a70b2 2017-11-06 stsp type = obj_types[i];
148 63323519 2017-11-06 stsp if (len <= tlen)
149 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
150 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
151 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
152 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
156 ab9a70b2 2017-11-06 stsp if (type == 0)
157 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
159 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
160 1db76ab5 2018-01-26 mpi if (*obj == NULL)
161 0a585a0d 2018-03-17 stsp return got_error_from_errno();
162 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
163 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
164 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
165 ab9a70b2 2017-11-06 stsp return NULL;
168 ab9a70b2 2017-11-06 stsp static const struct got_error *
169 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
172 ab9a70b2 2017-11-06 stsp const struct got_error *err;
173 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
175 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
176 744d9326 2017-12-01 stsp size_t outlen, totlen;
179 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
180 a3e2cbea 2017-12-01 stsp if (buf == NULL)
181 0a585a0d 2018-03-17 stsp return got_error_from_errno();
183 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
185 ab9a70b2 2017-11-06 stsp return err;
188 744d9326 2017-12-01 stsp totlen = 0;
190 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
193 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
194 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
195 e302c59e 2017-12-01 stsp if (buf == NULL) {
196 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
200 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
201 744d9326 2017-12-01 stsp totlen += outlen;
203 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
205 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
207 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
208 ab9a70b2 2017-11-06 stsp return err;
211 d1cda826 2017-11-06 stsp static const struct got_error *
212 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
214 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
216 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
218 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
219 0a585a0d 2018-03-17 stsp return got_error_from_errno();
221 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
223 ef0981d5 2018-02-12 stsp return err;
225 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
226 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
227 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
230 d1cda826 2017-11-06 stsp free(path_objects);
231 d1cda826 2017-11-06 stsp return err;
234 4ee4114f 2018-01-23 stsp static const struct got_error *
235 eb651edf 2018-02-11 stsp open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
237 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
238 a1fd68d8 2018-01-12 stsp char *path;
240 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
242 d1cda826 2017-11-06 stsp return err;
243 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
244 4558fcd4 2018-01-14 stsp if (*f == NULL) {
245 6c00b545 2018-01-17 stsp err = got_error_from_errno();
249 4558fcd4 2018-01-14 stsp free(path);
250 4558fcd4 2018-01-14 stsp return err;
253 4558fcd4 2018-01-14 stsp const struct got_error *
254 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
255 4558fcd4 2018-01-14 stsp struct got_object_id *id)
257 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
258 6c00b545 2018-01-17 stsp char *path;
261 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
263 4558fcd4 2018-01-14 stsp return err;
265 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
266 6c00b545 2018-01-17 stsp if (f == NULL) {
267 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
268 6c00b545 2018-01-17 stsp err = got_error_from_errno();
271 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
274 6c00b545 2018-01-17 stsp if (*obj == NULL)
275 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
277 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
280 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
283 6c00b545 2018-01-17 stsp free(path);
286 ab9a70b2 2017-11-06 stsp return err;
290 6dfa2fd3 2018-02-12 stsp const struct got_error *
291 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
292 6dfa2fd3 2018-02-12 stsp const char *id_str)
294 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
296 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
297 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
299 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
303 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
305 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
306 c3703302 2018-01-23 stsp struct got_delta *delta;
307 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
308 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
309 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
310 c3703302 2018-01-23 stsp got_delta_close(delta);
313 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
314 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
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 0a585a0d 2018-03-17 stsp return got_error_from_errno();
329 59ece79d 2018-02-12 stsp (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
330 59ece79d 2018-02-12 stsp if ((*commit)->tree_id == NULL) {
331 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
332 59ece79d 2018-02-12 stsp free(*commit);
333 59ece79d 2018-02-12 stsp *commit = NULL;
334 0a585a0d 2018-03-17 stsp return err;
337 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
339 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
340 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
341 d1cda826 2017-11-06 stsp remain -= tlen;
342 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
343 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
347 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
348 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
352 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
354 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
359 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
360 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
362 d1cda826 2017-11-06 stsp remain -= tlen;
363 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
364 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
368 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
369 d1cda826 2017-11-06 stsp if (pid == NULL) {
370 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
373 59ece79d 2018-02-12 stsp pid->id = calloc(1, sizeof(*pid->id));
374 59ece79d 2018-02-12 stsp if (pid->id == NULL) {
375 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
380 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest(pid->id->sha1, s)) {
381 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
382 59ece79d 2018-02-12 stsp free(pid->id);
386 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
387 d1cda826 2017-11-06 stsp (*commit)->nparents++;
389 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
390 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
393 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
394 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
397 d1cda826 2017-11-06 stsp remain -= tlen;
398 d1cda826 2017-11-06 stsp if (remain <= 0) {
399 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
403 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
404 d1cda826 2017-11-06 stsp if (p == NULL) {
405 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
409 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
410 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
411 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
414 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
415 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
418 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
419 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
422 d1cda826 2017-11-06 stsp remain -= tlen;
423 d1cda826 2017-11-06 stsp if (remain <= 0) {
424 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
429 d1cda826 2017-11-06 stsp if (p == NULL) {
430 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
434 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
435 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
436 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
439 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
440 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
443 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
444 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
445 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
450 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
451 59ece79d 2018-02-12 stsp *commit = NULL;
453 0ffeb3c2 2017-11-26 stsp return err;
456 0ffeb3c2 2017-11-26 stsp static void
457 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
459 59ece79d 2018-02-12 stsp free(te->id);
460 0ffeb3c2 2017-11-26 stsp free(te->name);
464 0ffeb3c2 2017-11-26 stsp static const struct got_error *
465 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
466 0ffeb3c2 2017-11-26 stsp size_t maxlen)
468 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
469 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
471 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
472 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
473 0a585a0d 2018-03-17 stsp return got_error_from_errno();
475 59ece79d 2018-02-12 stsp (*te)->id = calloc(1, sizeof(*(*te)->id));
476 59ece79d 2018-02-12 stsp if ((*te)->id == NULL) {
477 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
479 59ece79d 2018-02-12 stsp *te = NULL;
480 0a585a0d 2018-03-17 stsp return err;
483 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
484 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
486 59ece79d 2018-02-12 stsp *te = NULL;
487 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
490 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
491 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
492 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 59ece79d 2018-02-12 stsp *te = NULL;
495 0a585a0d 2018-03-17 stsp return err;
497 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
498 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
499 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
502 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
503 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
507 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
508 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
509 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
512 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
513 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
514 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
517 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
518 59ece79d 2018-02-12 stsp *te = NULL;
520 d1cda826 2017-11-06 stsp return err;
523 d1cda826 2017-11-06 stsp static const struct got_error *
524 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
525 e0ab43e7 2018-03-16 stsp uint8_t *buf, size_t len)
527 90356acc 2018-01-27 stsp const struct got_error *err;
528 0ffeb3c2 2017-11-26 stsp size_t remain = len;
530 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
531 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
532 0a585a0d 2018-03-17 stsp return got_error_from_errno();
534 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
536 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
537 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
538 0ffeb3c2 2017-11-26 stsp size_t elen;
540 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
542 90356acc 2018-01-27 stsp return err;
543 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
544 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
545 0ffeb3c2 2017-11-26 stsp buf += elen;
546 0ffeb3c2 2017-11-26 stsp remain -= elen;
549 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
550 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
551 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
554 0ffeb3c2 2017-11-26 stsp return NULL;
557 0ffeb3c2 2017-11-26 stsp static const struct got_error *
558 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
560 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
561 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
562 eb651edf 2018-02-11 stsp size_t n, total, remain;
563 eb651edf 2018-02-11 stsp uint8_t *buf;
565 eb651edf 2018-02-11 stsp *outbuf = NULL;
566 eb651edf 2018-02-11 stsp *outlen = 0;
568 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
569 eb651edf 2018-02-11 stsp if (buf == NULL)
570 0a585a0d 2018-03-17 stsp return got_error_from_errno();
572 eb651edf 2018-02-11 stsp remain = blocksize;
574 eb651edf 2018-02-11 stsp while (1) {
575 eb651edf 2018-02-11 stsp if (remain == 0) {
576 eb651edf 2018-02-11 stsp uint8_t *newbuf;
577 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
578 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
579 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
582 eb651edf 2018-02-11 stsp buf = newbuf;
583 eb651edf 2018-02-11 stsp remain += blocksize;
585 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
586 eb651edf 2018-02-11 stsp if (n == 0) {
587 eb651edf 2018-02-11 stsp if (ferror(f)) {
588 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
591 eb651edf 2018-02-11 stsp break; /* EOF */
593 eb651edf 2018-02-11 stsp remain -= n;
594 eb651edf 2018-02-11 stsp total += n;
598 eb651edf 2018-02-11 stsp if (err == NULL) {
599 eb651edf 2018-02-11 stsp *outbuf = buf;
600 eb651edf 2018-02-11 stsp *outlen = total;
603 eb651edf 2018-02-11 stsp return err;
606 eb651edf 2018-02-11 stsp static const struct got_error *
607 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
608 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
610 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
611 d1cda826 2017-11-06 stsp size_t len;
612 eb651edf 2018-02-11 stsp uint8_t *p;
614 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
615 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
617 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
619 d1cda826 2017-11-06 stsp return err;
621 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
622 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 d1cda826 2017-11-06 stsp /* Skip object header. */
627 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
628 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
631 d1cda826 2017-11-06 stsp return err;
634 d1cda826 2017-11-06 stsp const struct got_error *
635 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
636 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
638 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
640 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
641 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
643 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
644 ea35256b 2018-03-16 stsp uint8_t *buf;
645 ea35256b 2018-03-16 stsp size_t len;
646 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
648 ea35256b 2018-03-16 stsp return err;
649 b29656e2 2018-03-16 stsp obj->size = len;
650 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
654 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
656 ea35256b 2018-03-16 stsp return err;
657 ea35256b 2018-03-16 stsp err = read_commit_object(commit, repo, obj, f);
660 d1cda826 2017-11-06 stsp return err;
664 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
666 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
668 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
669 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
670 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
671 59ece79d 2018-02-12 stsp free(pid->id);
675 59ece79d 2018-02-12 stsp free(commit->tree_id);
676 d1cda826 2017-11-06 stsp free(commit->author);
677 d1cda826 2017-11-06 stsp free(commit->committer);
678 d1cda826 2017-11-06 stsp free(commit->logmsg);
679 d1cda826 2017-11-06 stsp free(commit);
682 0ffeb3c2 2017-11-26 stsp static const struct got_error *
683 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
684 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
686 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
687 0ffeb3c2 2017-11-26 stsp size_t len;
688 eb651edf 2018-02-11 stsp uint8_t *p;
690 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
691 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
693 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
695 0ffeb3c2 2017-11-26 stsp return err;
697 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
698 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
702 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
703 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
704 eb651edf 2018-02-11 stsp err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
707 0ffeb3c2 2017-11-26 stsp return err;
710 0ffeb3c2 2017-11-26 stsp const struct got_error *
711 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
712 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
714 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
716 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
717 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
719 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
720 e0ab43e7 2018-03-16 stsp uint8_t *buf;
721 e0ab43e7 2018-03-16 stsp size_t len;
722 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
724 e0ab43e7 2018-03-16 stsp return err;
725 b29656e2 2018-03-16 stsp obj->size = len;
726 b29656e2 2018-03-16 stsp err = parse_tree_object(tree, repo, buf, len);
730 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
732 e0ab43e7 2018-03-16 stsp return err;
733 e0ab43e7 2018-03-16 stsp err = read_tree_object(tree, repo, obj, f);
736 0ffeb3c2 2017-11-26 stsp return err;
740 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
742 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
744 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
745 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
746 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
747 f715ca7f 2017-11-27 stsp tree_entry_close(te);
750 f715ca7f 2017-11-27 stsp free(tree);
753 68482ea3 2017-11-27 stsp const struct got_error *
754 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
755 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
757 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
759 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
760 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
762 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
763 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
765 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
766 4558fcd4 2018-01-14 stsp if (*blob == NULL)
767 0a585a0d 2018-03-17 stsp return got_error_from_errno();
769 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
770 eb651edf 2018-02-11 stsp (*blob)->read_buf = calloc(1, blocksize);
771 56866f4a 2018-03-17 stsp if ((*blob)->read_buf == NULL) {
772 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
773 56866f4a 2018-03-17 stsp free(*blob);
774 56866f4a 2018-03-17 stsp *blob = NULL;
775 0a585a0d 2018-03-17 stsp return err;
777 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
779 56866f4a 2018-03-17 stsp free((*blob)->read_buf);
780 56866f4a 2018-03-17 stsp free(*blob);
781 56866f4a 2018-03-17 stsp *blob = NULL;
782 eb651edf 2018-02-11 stsp return err;
785 eb651edf 2018-02-11 stsp err = open_loose_object(&((*blob)->f), obj, repo);
787 eb651edf 2018-02-11 stsp free(*blob);
788 d0f3be7c 2018-03-17 stsp *blob = NULL;
789 eb651edf 2018-02-11 stsp return err;
792 19d747f7 2018-03-16 stsp err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
793 eb651edf 2018-02-11 stsp if (err != NULL) {
794 eb651edf 2018-02-11 stsp fclose((*blob)->f);
795 eb651edf 2018-02-11 stsp free(*blob);
796 56866f4a 2018-03-17 stsp *blob = NULL;
797 eb651edf 2018-02-11 stsp return err;
800 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
801 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
804 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
805 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
806 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
808 68482ea3 2017-11-27 stsp return err;
812 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
814 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
815 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
817 eb651edf 2018-02-11 stsp free(blob->read_buf);
818 68482ea3 2017-11-27 stsp fclose(blob->f);
819 68482ea3 2017-11-27 stsp free(blob);
823 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
825 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
829 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
831 f934cf2c 2018-02-12 stsp return blob->hdrlen;
834 f934cf2c 2018-02-12 stsp const uint8_t *
835 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
837 f934cf2c 2018-02-12 stsp return blob->read_buf;
840 68482ea3 2017-11-27 stsp const struct got_error *
841 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
845 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
846 eb651edf 2018-02-11 stsp return got_inflate_read(&blob->zb, blob->f, outlenp);
848 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
849 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
850 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
851 eb651edf 2018-02-11 stsp *outlenp = n;
852 eb651edf 2018-02-11 stsp return NULL;