Blame


1 d71d75ad 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
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.
7 d71d75ad 2017-11-05 stsp *
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.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
17 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
18 d1cda826 2017-11-06 stsp #include <sys/queue.h>
19 d1cda826 2017-11-06 stsp
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>
28 d71d75ad 2017-11-05 stsp
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 d71d75ad 2017-11-05 stsp
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"
38 1411938b 2018-02-12 stsp
39 ab9a70b2 2017-11-06 stsp #ifndef MIN
40 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
41 ab9a70b2 2017-11-06 stsp #endif
42 ab9a70b2 2017-11-06 stsp
43 ab9a70b2 2017-11-06 stsp #ifndef nitems
44 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
45 ab9a70b2 2017-11-06 stsp #endif
46 ab9a70b2 2017-11-06 stsp
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"
50 ab9a70b2 2017-11-06 stsp
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 "
55 d1cda826 2017-11-06 stsp
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)
58 d71d75ad 2017-11-05 stsp {
59 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
60 ef0981d5 2018-02-12 stsp
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);
64 ef0981d5 2018-02-12 stsp
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);
69 ef0981d5 2018-02-12 stsp }
70 ef0981d5 2018-02-12 stsp
71 ef0981d5 2018-02-12 stsp return NULL;
72 59ece79d 2018-02-12 stsp }
73 59ece79d 2018-02-12 stsp
74 a1fd68d8 2018-01-12 stsp int
75 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
76 a1fd68d8 2018-01-12 stsp {
77 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
78 a1fd68d8 2018-01-12 stsp }
79 d71d75ad 2017-11-05 stsp
80 b107e67f 2018-01-19 stsp int
81 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
82 a1fd68d8 2018-01-12 stsp {
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;
89 96f5e8b3 2018-01-23 stsp default:
90 96f5e8b3 2018-01-23 stsp abort();
91 96f5e8b3 2018-01-23 stsp break;
92 d71d75ad 2017-11-05 stsp }
93 d71d75ad 2017-11-05 stsp
94 96f5e8b3 2018-01-23 stsp /* not reached */
95 96f5e8b3 2018-01-23 stsp return 0;
96 d71d75ad 2017-11-05 stsp }
97 ab9a70b2 2017-11-06 stsp
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)
100 ab9a70b2 2017-11-06 stsp {
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
105 ab9a70b2 2017-11-06 stsp };
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,
110 ab9a70b2 2017-11-06 stsp };
111 ab9a70b2 2017-11-06 stsp int type = 0;
112 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
113 ab9a70b2 2017-11-06 stsp int i;
114 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
115 ab9a70b2 2017-11-06 stsp
116 ab9a70b2 2017-11-06 stsp if (p == NULL)
117 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
118 ab9a70b2 2017-11-06 stsp
119 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
120 d1cda826 2017-11-06 stsp
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;
125 ab9a70b2 2017-11-06 stsp
126 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
127 ab9a70b2 2017-11-06 stsp continue;
128 ab9a70b2 2017-11-06 stsp
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);
135 ab9a70b2 2017-11-06 stsp break;
136 ab9a70b2 2017-11-06 stsp }
137 ab9a70b2 2017-11-06 stsp
138 ab9a70b2 2017-11-06 stsp if (type == 0)
139 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
140 ab9a70b2 2017-11-06 stsp
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;
148 ab9a70b2 2017-11-06 stsp }
149 ab9a70b2 2017-11-06 stsp
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,
152 a1fd68d8 2018-01-12 stsp FILE *f)
153 ab9a70b2 2017-11-06 stsp {
154 ab9a70b2 2017-11-06 stsp const struct got_error *err;
155 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
156 a3e2cbea 2017-12-01 stsp char *buf;
157 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
158 744d9326 2017-12-01 stsp size_t outlen, totlen;
159 25783624 2018-03-12 stsp int i;
160 ab9a70b2 2017-11-06 stsp
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);
164 a3e2cbea 2017-12-01 stsp
165 4ca7b755 2018-01-26 stsp err = got_inflate_init(&zb, zbsize);
166 a1fd68d8 2018-01-12 stsp if (err)
167 ab9a70b2 2017-11-06 stsp return err;
168 ab9a70b2 2017-11-06 stsp
169 a3e2cbea 2017-12-01 stsp i = 0;
170 744d9326 2017-12-01 stsp totlen = 0;
171 a3e2cbea 2017-12-01 stsp do {
172 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
173 a3e2cbea 2017-12-01 stsp if (err)
174 a3e2cbea 2017-12-01 stsp goto done;
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);
179 e302c59e 2017-12-01 stsp goto done;
180 e302c59e 2017-12-01 stsp }
181 e302c59e 2017-12-01 stsp }
182 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
183 744d9326 2017-12-01 stsp totlen += outlen;
184 a3e2cbea 2017-12-01 stsp i++;
185 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
186 ab9a70b2 2017-11-06 stsp
187 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
188 ab9a70b2 2017-11-06 stsp done:
189 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
190 ab9a70b2 2017-11-06 stsp return err;
191 ab9a70b2 2017-11-06 stsp }
192 ab9a70b2 2017-11-06 stsp
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)
195 ab9a70b2 2017-11-06 stsp {
196 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
197 ef0981d5 2018-02-12 stsp char *hex;
198 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
199 ab9a70b2 2017-11-06 stsp
200 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
201 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_NO_MEM);
202 ab9a70b2 2017-11-06 stsp
203 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
204 ef0981d5 2018-02-12 stsp if (err)
205 ef0981d5 2018-02-12 stsp return err;
206 ab9a70b2 2017-11-06 stsp
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);
210 ab9a70b2 2017-11-06 stsp
211 ef0981d5 2018-02-12 stsp free(hex);
212 d1cda826 2017-11-06 stsp free(path_objects);
213 d1cda826 2017-11-06 stsp return err;
214 d1cda826 2017-11-06 stsp }
215 d1cda826 2017-11-06 stsp
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)
218 d1cda826 2017-11-06 stsp {
219 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
220 a1fd68d8 2018-01-12 stsp char *path;
221 6c00b545 2018-01-17 stsp
222 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
223 d1cda826 2017-11-06 stsp if (err)
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();
228 6c00b545 2018-01-17 stsp goto done;
229 a1fd68d8 2018-01-12 stsp }
230 4558fcd4 2018-01-14 stsp done:
231 4558fcd4 2018-01-14 stsp free(path);
232 4558fcd4 2018-01-14 stsp return err;
233 4558fcd4 2018-01-14 stsp }
234 a1fd68d8 2018-01-12 stsp
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)
238 4558fcd4 2018-01-14 stsp {
239 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
240 6c00b545 2018-01-17 stsp char *path;
241 4558fcd4 2018-01-14 stsp FILE *f;
242 4558fcd4 2018-01-14 stsp
243 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
244 4558fcd4 2018-01-14 stsp if (err)
245 4558fcd4 2018-01-14 stsp return err;
246 4558fcd4 2018-01-14 stsp
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();
251 6c00b545 2018-01-17 stsp goto done;
252 6c00b545 2018-01-17 stsp }
253 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
254 6c00b545 2018-01-17 stsp if (err)
255 6c00b545 2018-01-17 stsp goto done;
256 6c00b545 2018-01-17 stsp if (*obj == NULL)
257 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
258 6c00b545 2018-01-17 stsp } else {
259 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
260 6c00b545 2018-01-17 stsp if (err)
261 6c00b545 2018-01-17 stsp goto done;
262 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
263 6c00b545 2018-01-17 stsp }
264 6c00b545 2018-01-17 stsp done:
265 6c00b545 2018-01-17 stsp free(path);
266 6c00b545 2018-01-17 stsp if (err && f)
267 a1fd68d8 2018-01-12 stsp fclose(f);
268 ab9a70b2 2017-11-06 stsp return err;
269 6c00b545 2018-01-17 stsp
270 ab9a70b2 2017-11-06 stsp }
271 ab9a70b2 2017-11-06 stsp
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)
275 6dfa2fd3 2018-02-12 stsp {
276 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
277 6dfa2fd3 2018-02-12 stsp
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);
280 6dfa2fd3 2018-02-12 stsp
281 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
282 6dfa2fd3 2018-02-12 stsp }
283 6dfa2fd3 2018-02-12 stsp
284 ab9a70b2 2017-11-06 stsp void
285 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
286 ab9a70b2 2017-11-06 stsp {
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);
293 96f5e8b3 2018-01-23 stsp }
294 96f5e8b3 2018-01-23 stsp }
295 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
296 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
297 ab9a70b2 2017-11-06 stsp free(obj);
298 d1cda826 2017-11-06 stsp }
299 d1cda826 2017-11-06 stsp
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)
302 d1cda826 2017-11-06 stsp {
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;
307 d1cda826 2017-11-06 stsp
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);
316 59ece79d 2018-02-12 stsp }
317 d1cda826 2017-11-06 stsp
318 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
319 d1cda826 2017-11-06 stsp
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);
325 d1cda826 2017-11-06 stsp goto done;
326 d1cda826 2017-11-06 stsp }
327 d1cda826 2017-11-06 stsp s += tlen;
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);
330 d1cda826 2017-11-06 stsp goto done;
331 d1cda826 2017-11-06 stsp }
332 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
333 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
334 d1cda826 2017-11-06 stsp } else {
335 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 d1cda826 2017-11-06 stsp goto done;
337 d1cda826 2017-11-06 stsp }
338 d1cda826 2017-11-06 stsp
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;
342 d1cda826 2017-11-06 stsp
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);
346 d1cda826 2017-11-06 stsp goto done;
347 ef0981d5 2018-02-12 stsp }
348 d1cda826 2017-11-06 stsp
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);
352 d1cda826 2017-11-06 stsp goto done;
353 d1cda826 2017-11-06 stsp }
354 59ece79d 2018-02-12 stsp pid->id = calloc(1, sizeof(*pid->id));
355 59ece79d 2018-02-12 stsp if (pid->id == NULL) {
356 59ece79d 2018-02-12 stsp free(pid);
357 59ece79d 2018-02-12 stsp err = got_error(GOT_ERR_NO_MEM);
358 59ece79d 2018-02-12 stsp goto done;
359 59ece79d 2018-02-12 stsp }
360 59ece79d 2018-02-12 stsp s += tlen;
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);
364 59ece79d 2018-02-12 stsp free(pid);
365 d1cda826 2017-11-06 stsp goto done;
366 d1cda826 2017-11-06 stsp }
367 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
368 d1cda826 2017-11-06 stsp (*commit)->nparents++;
369 d1cda826 2017-11-06 stsp
370 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
371 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
372 d1cda826 2017-11-06 stsp }
373 d1cda826 2017-11-06 stsp
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) {
376 d1cda826 2017-11-06 stsp char *p;
377 d1cda826 2017-11-06 stsp
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);
381 d1cda826 2017-11-06 stsp goto done;
382 d1cda826 2017-11-06 stsp }
383 d1cda826 2017-11-06 stsp s += tlen;
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);
387 d1cda826 2017-11-06 stsp goto done;
388 d1cda826 2017-11-06 stsp }
389 d1cda826 2017-11-06 stsp *p = '\0';
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);
393 d1cda826 2017-11-06 stsp goto done;
394 d1cda826 2017-11-06 stsp }
395 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
396 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
397 d1cda826 2017-11-06 stsp }
398 d1cda826 2017-11-06 stsp
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) {
401 d1cda826 2017-11-06 stsp char *p;
402 d1cda826 2017-11-06 stsp
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);
406 d1cda826 2017-11-06 stsp goto done;
407 d1cda826 2017-11-06 stsp }
408 d1cda826 2017-11-06 stsp s += tlen;
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);
412 d1cda826 2017-11-06 stsp goto done;
413 d1cda826 2017-11-06 stsp }
414 d1cda826 2017-11-06 stsp *p = '\0';
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);
418 d1cda826 2017-11-06 stsp goto done;
419 d1cda826 2017-11-06 stsp }
420 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
421 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
422 d1cda826 2017-11-06 stsp }
423 d1cda826 2017-11-06 stsp
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);
427 eb651edf 2018-02-11 stsp goto done;
428 eb651edf 2018-02-11 stsp }
429 d1cda826 2017-11-06 stsp done:
430 59ece79d 2018-02-12 stsp if (err) {
431 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
432 59ece79d 2018-02-12 stsp *commit = NULL;
433 59ece79d 2018-02-12 stsp }
434 0ffeb3c2 2017-11-26 stsp return err;
435 0ffeb3c2 2017-11-26 stsp }
436 0ffeb3c2 2017-11-26 stsp
437 0ffeb3c2 2017-11-26 stsp static void
438 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
439 0ffeb3c2 2017-11-26 stsp {
440 59ece79d 2018-02-12 stsp free(te->id);
441 0ffeb3c2 2017-11-26 stsp free(te->name);
442 0ffeb3c2 2017-11-26 stsp free(te);
443 0ffeb3c2 2017-11-26 stsp }
444 0ffeb3c2 2017-11-26 stsp
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)
448 0ffeb3c2 2017-11-26 stsp {
449 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
450 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
451 0ffeb3c2 2017-11-26 stsp
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);
455 59ece79d 2018-02-12 stsp
456 59ece79d 2018-02-12 stsp (*te)->id = calloc(1, sizeof(*(*te)->id));
457 59ece79d 2018-02-12 stsp if ((*te)->id == NULL) {
458 59ece79d 2018-02-12 stsp free(*te);
459 59ece79d 2018-02-12 stsp *te = NULL;
460 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_NO_MEM);
461 59ece79d 2018-02-12 stsp }
462 0ffeb3c2 2017-11-26 stsp
463 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
464 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
465 0ffeb3c2 2017-11-26 stsp free(*te);
466 59ece79d 2018-02-12 stsp *te = NULL;
467 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
468 0ffeb3c2 2017-11-26 stsp }
469 0ffeb3c2 2017-11-26 stsp
470 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
471 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
472 0ffeb3c2 2017-11-26 stsp free(*te);
473 59ece79d 2018-02-12 stsp *te = NULL;
474 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
475 0ffeb3c2 2017-11-26 stsp }
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);
479 0ffeb3c2 2017-11-26 stsp goto done;
480 0ffeb3c2 2017-11-26 stsp }
481 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
482 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
483 0ffeb3c2 2017-11-26 stsp p++;
484 0ffeb3c2 2017-11-26 stsp }
485 0ffeb3c2 2017-11-26 stsp
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);
489 0ffeb3c2 2017-11-26 stsp goto done;
490 0ffeb3c2 2017-11-26 stsp }
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;
494 0ffeb3c2 2017-11-26 stsp done:
495 59ece79d 2018-02-12 stsp if (err) {
496 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
497 59ece79d 2018-02-12 stsp *te = NULL;
498 59ece79d 2018-02-12 stsp }
499 d1cda826 2017-11-06 stsp return err;
500 d1cda826 2017-11-06 stsp }
501 d1cda826 2017-11-06 stsp
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)
505 0ffeb3c2 2017-11-26 stsp {
506 90356acc 2018-01-27 stsp const struct got_error *err;
507 0ffeb3c2 2017-11-26 stsp size_t remain = len;
508 0ffeb3c2 2017-11-26 stsp
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);
512 0ffeb3c2 2017-11-26 stsp
513 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
514 0ffeb3c2 2017-11-26 stsp
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;
518 0ffeb3c2 2017-11-26 stsp
519 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
520 90356acc 2018-01-27 stsp if (err)
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;
526 0ffeb3c2 2017-11-26 stsp }
527 0ffeb3c2 2017-11-26 stsp
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);
531 0ffeb3c2 2017-11-26 stsp }
532 0ffeb3c2 2017-11-26 stsp
533 0ffeb3c2 2017-11-26 stsp return NULL;
534 0ffeb3c2 2017-11-26 stsp }
535 0ffeb3c2 2017-11-26 stsp
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)
538 eb651edf 2018-02-11 stsp {
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;
543 eb651edf 2018-02-11 stsp
544 eb651edf 2018-02-11 stsp *outbuf = NULL;
545 eb651edf 2018-02-11 stsp *outlen = 0;
546 eb651edf 2018-02-11 stsp
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);
550 eb651edf 2018-02-11 stsp
551 eb651edf 2018-02-11 stsp remain = blocksize;
552 eb651edf 2018-02-11 stsp total = 0;
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);
559 eb651edf 2018-02-11 stsp goto done;
560 eb651edf 2018-02-11 stsp }
561 eb651edf 2018-02-11 stsp buf = newbuf;
562 eb651edf 2018-02-11 stsp remain += blocksize;
563 eb651edf 2018-02-11 stsp }
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);
568 eb651edf 2018-02-11 stsp goto done;
569 eb651edf 2018-02-11 stsp }
570 eb651edf 2018-02-11 stsp break; /* EOF */
571 eb651edf 2018-02-11 stsp }
572 eb651edf 2018-02-11 stsp remain -= n;
573 eb651edf 2018-02-11 stsp total += n;
574 eb651edf 2018-02-11 stsp };
575 eb651edf 2018-02-11 stsp
576 eb651edf 2018-02-11 stsp done:
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;
580 eb651edf 2018-02-11 stsp } else
581 eb651edf 2018-02-11 stsp free(buf);
582 eb651edf 2018-02-11 stsp return err;
583 eb651edf 2018-02-11 stsp }
584 eb651edf 2018-02-11 stsp
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)
588 d1cda826 2017-11-06 stsp {
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;
592 d1cda826 2017-11-06 stsp
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);
595 eb651edf 2018-02-11 stsp else
596 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
597 4558fcd4 2018-01-14 stsp if (err)
598 d1cda826 2017-11-06 stsp return err;
599 d1cda826 2017-11-06 stsp
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);
602 d1cda826 2017-11-06 stsp goto done;
603 d1cda826 2017-11-06 stsp }
604 d1cda826 2017-11-06 stsp
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);
608 eb651edf 2018-02-11 stsp free(p);
609 d1cda826 2017-11-06 stsp done:
610 d1cda826 2017-11-06 stsp return err;
611 d1cda826 2017-11-06 stsp }
612 d1cda826 2017-11-06 stsp
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)
616 d1cda826 2017-11-06 stsp {
617 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
618 4558fcd4 2018-01-14 stsp FILE *f;
619 d1cda826 2017-11-06 stsp
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);
622 d1cda826 2017-11-06 stsp
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);
625 eb651edf 2018-02-11 stsp else
626 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
627 d1cda826 2017-11-06 stsp if (err)
628 d1cda826 2017-11-06 stsp return err;
629 d1cda826 2017-11-06 stsp
630 4558fcd4 2018-01-14 stsp err = read_commit_object(commit, repo, obj, f);
631 4558fcd4 2018-01-14 stsp fclose(f);
632 d1cda826 2017-11-06 stsp return err;
633 d1cda826 2017-11-06 stsp }
634 d1cda826 2017-11-06 stsp
635 d1cda826 2017-11-06 stsp void
636 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
637 d1cda826 2017-11-06 stsp {
638 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
639 d1cda826 2017-11-06 stsp
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);
644 d1cda826 2017-11-06 stsp free(pid);
645 d1cda826 2017-11-06 stsp }
646 d1cda826 2017-11-06 stsp
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);
652 d1cda826 2017-11-06 stsp }
653 0ffeb3c2 2017-11-26 stsp
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)
657 0ffeb3c2 2017-11-26 stsp {
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;
661 0ffeb3c2 2017-11-26 stsp
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);
664 eb651edf 2018-02-11 stsp else
665 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
666 4558fcd4 2018-01-14 stsp if (err)
667 0ffeb3c2 2017-11-26 stsp return err;
668 0ffeb3c2 2017-11-26 stsp
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);
671 0ffeb3c2 2017-11-26 stsp goto done;
672 0ffeb3c2 2017-11-26 stsp }
673 0ffeb3c2 2017-11-26 stsp
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);
677 eb651edf 2018-02-11 stsp free(p);
678 0ffeb3c2 2017-11-26 stsp done:
679 0ffeb3c2 2017-11-26 stsp return err;
680 0ffeb3c2 2017-11-26 stsp }
681 0ffeb3c2 2017-11-26 stsp
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)
685 0ffeb3c2 2017-11-26 stsp {
686 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
687 4558fcd4 2018-01-14 stsp FILE *f;
688 0ffeb3c2 2017-11-26 stsp
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);
691 0ffeb3c2 2017-11-26 stsp
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);
694 eb651edf 2018-02-11 stsp else
695 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
696 0ffeb3c2 2017-11-26 stsp if (err)
697 0ffeb3c2 2017-11-26 stsp return err;
698 0ffeb3c2 2017-11-26 stsp
699 4558fcd4 2018-01-14 stsp err = read_tree_object(tree, repo, obj, f);
700 4558fcd4 2018-01-14 stsp fclose(f);
701 0ffeb3c2 2017-11-26 stsp return err;
702 0ffeb3c2 2017-11-26 stsp }
703 0ffeb3c2 2017-11-26 stsp
704 0ffeb3c2 2017-11-26 stsp void
705 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
706 0ffeb3c2 2017-11-26 stsp {
707 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
708 f715ca7f 2017-11-27 stsp
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);
713 f715ca7f 2017-11-27 stsp }
714 f715ca7f 2017-11-27 stsp
715 f715ca7f 2017-11-27 stsp free(tree);
716 68482ea3 2017-11-27 stsp }
717 68482ea3 2017-11-27 stsp
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)
721 68482ea3 2017-11-27 stsp {
722 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
723 68482ea3 2017-11-27 stsp
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);
726 68482ea3 2017-11-27 stsp
727 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
728 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
729 7d283eee 2017-11-29 stsp
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);
733 68482ea3 2017-11-27 stsp
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);
739 eb651edf 2018-02-11 stsp if (err)
740 eb651edf 2018-02-11 stsp return err;
741 eb651edf 2018-02-11 stsp } else {
742 eb651edf 2018-02-11 stsp err = open_loose_object(&((*blob)->f), obj, repo);
743 eb651edf 2018-02-11 stsp if (err) {
744 eb651edf 2018-02-11 stsp free(*blob);
745 eb651edf 2018-02-11 stsp return err;
746 eb651edf 2018-02-11 stsp }
747 68482ea3 2017-11-27 stsp
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;
753 eb651edf 2018-02-11 stsp }
754 eb651edf 2018-02-11 stsp
755 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
756 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
757 68482ea3 2017-11-27 stsp }
758 68482ea3 2017-11-27 stsp
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);
762 7d283eee 2017-11-29 stsp
763 68482ea3 2017-11-27 stsp return err;
764 0ffeb3c2 2017-11-26 stsp }
765 68482ea3 2017-11-27 stsp
766 68482ea3 2017-11-27 stsp void
767 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
768 68482ea3 2017-11-27 stsp {
769 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
770 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
771 eb651edf 2018-02-11 stsp else
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);
775 f934cf2c 2018-02-12 stsp }
776 f934cf2c 2018-02-12 stsp
777 f934cf2c 2018-02-12 stsp char *
778 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
779 f934cf2c 2018-02-12 stsp {
780 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
781 f934cf2c 2018-02-12 stsp }
782 f934cf2c 2018-02-12 stsp
783 f934cf2c 2018-02-12 stsp size_t
784 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
785 f934cf2c 2018-02-12 stsp {
786 f934cf2c 2018-02-12 stsp return blob->hdrlen;
787 68482ea3 2017-11-27 stsp }
788 68482ea3 2017-11-27 stsp
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)
791 f934cf2c 2018-02-12 stsp {
792 f934cf2c 2018-02-12 stsp return blob->read_buf;
793 f934cf2c 2018-02-12 stsp }
794 f934cf2c 2018-02-12 stsp
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)
797 68482ea3 2017-11-27 stsp {
798 eb651edf 2018-02-11 stsp size_t n;
799 eb651edf 2018-02-11 stsp
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);
802 eb651edf 2018-02-11 stsp
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;
808 68482ea3 2017-11-27 stsp }