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 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"
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 0a585a0d 2018-03-17 stsp return got_error_from_errno();
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 8bf5b3c9 2018-03-17 stsp }
79 8bf5b3c9 2018-03-17 stsp
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)
82 8bf5b3c9 2018-03-17 stsp {
83 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
84 8bf5b3c9 2018-03-17 stsp
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));
89 8bf5b3c9 2018-03-17 stsp return id2;
90 a1fd68d8 2018-01-12 stsp }
91 d71d75ad 2017-11-05 stsp
92 b107e67f 2018-01-19 stsp int
93 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
94 a1fd68d8 2018-01-12 stsp {
95 b107e67f 2018-01-19 stsp switch (obj->type) {
96 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
97 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
98 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
99 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
100 b107e67f 2018-01-19 stsp return obj->type;
101 96f5e8b3 2018-01-23 stsp default:
102 96f5e8b3 2018-01-23 stsp abort();
103 96f5e8b3 2018-01-23 stsp break;
104 d71d75ad 2017-11-05 stsp }
105 d71d75ad 2017-11-05 stsp
106 96f5e8b3 2018-01-23 stsp /* not reached */
107 96f5e8b3 2018-01-23 stsp return 0;
108 d71d75ad 2017-11-05 stsp }
109 ab9a70b2 2017-11-06 stsp
110 ab9a70b2 2017-11-06 stsp static const struct got_error *
111 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
112 ab9a70b2 2017-11-06 stsp {
113 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
114 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
115 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
116 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
117 ab9a70b2 2017-11-06 stsp };
118 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
119 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
120 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
121 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
122 ab9a70b2 2017-11-06 stsp };
123 ab9a70b2 2017-11-06 stsp int type = 0;
124 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
125 ab9a70b2 2017-11-06 stsp int i;
126 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
127 ab9a70b2 2017-11-06 stsp
128 ab9a70b2 2017-11-06 stsp if (p == NULL)
129 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
130 ab9a70b2 2017-11-06 stsp
131 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
132 d1cda826 2017-11-06 stsp
133 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
134 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
135 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
136 ab9a70b2 2017-11-06 stsp const char *errstr;
137 ab9a70b2 2017-11-06 stsp
138 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
139 ab9a70b2 2017-11-06 stsp continue;
140 ab9a70b2 2017-11-06 stsp
141 ab9a70b2 2017-11-06 stsp type = obj_types[i];
142 63323519 2017-11-06 stsp if (len <= tlen)
143 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
144 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
145 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
146 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
147 ab9a70b2 2017-11-06 stsp break;
148 ab9a70b2 2017-11-06 stsp }
149 ab9a70b2 2017-11-06 stsp
150 ab9a70b2 2017-11-06 stsp if (type == 0)
151 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
152 ab9a70b2 2017-11-06 stsp
153 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
154 1db76ab5 2018-01-26 mpi if (*obj == NULL)
155 0a585a0d 2018-03-17 stsp return got_error_from_errno();
156 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
157 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
158 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
159 ab9a70b2 2017-11-06 stsp return NULL;
160 ab9a70b2 2017-11-06 stsp }
161 ab9a70b2 2017-11-06 stsp
162 ab9a70b2 2017-11-06 stsp static const struct got_error *
163 ab9a70b2 2017-11-06 stsp read_object_header(struct got_object **obj, struct got_repository *repo,
164 a1fd68d8 2018-01-12 stsp FILE *f)
165 ab9a70b2 2017-11-06 stsp {
166 ab9a70b2 2017-11-06 stsp const struct got_error *err;
167 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
168 a3e2cbea 2017-12-01 stsp char *buf;
169 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
170 744d9326 2017-12-01 stsp size_t outlen, totlen;
171 25783624 2018-03-12 stsp int i;
172 ab9a70b2 2017-11-06 stsp
173 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
174 a3e2cbea 2017-12-01 stsp if (buf == NULL)
175 0a585a0d 2018-03-17 stsp return got_error_from_errno();
176 a3e2cbea 2017-12-01 stsp
177 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
178 a1fd68d8 2018-01-12 stsp if (err)
179 ab9a70b2 2017-11-06 stsp return err;
180 ab9a70b2 2017-11-06 stsp
181 a3e2cbea 2017-12-01 stsp i = 0;
182 744d9326 2017-12-01 stsp totlen = 0;
183 a3e2cbea 2017-12-01 stsp do {
184 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
185 a3e2cbea 2017-12-01 stsp if (err)
186 a3e2cbea 2017-12-01 stsp goto done;
187 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
188 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
189 e302c59e 2017-12-01 stsp if (buf == NULL) {
190 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
191 e302c59e 2017-12-01 stsp goto done;
192 e302c59e 2017-12-01 stsp }
193 e302c59e 2017-12-01 stsp }
194 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
195 744d9326 2017-12-01 stsp totlen += outlen;
196 a3e2cbea 2017-12-01 stsp i++;
197 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
198 ab9a70b2 2017-11-06 stsp
199 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
200 ab9a70b2 2017-11-06 stsp done:
201 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
202 ab9a70b2 2017-11-06 stsp return err;
203 ab9a70b2 2017-11-06 stsp }
204 ab9a70b2 2017-11-06 stsp
205 d1cda826 2017-11-06 stsp static const struct got_error *
206 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
207 ab9a70b2 2017-11-06 stsp {
208 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
209 ef0981d5 2018-02-12 stsp char *hex;
210 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
211 ab9a70b2 2017-11-06 stsp
212 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
213 0a585a0d 2018-03-17 stsp return got_error_from_errno();
214 ab9a70b2 2017-11-06 stsp
215 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
216 ef0981d5 2018-02-12 stsp if (err)
217 ef0981d5 2018-02-12 stsp return err;
218 ab9a70b2 2017-11-06 stsp
219 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
220 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
221 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
222 ab9a70b2 2017-11-06 stsp
223 ef0981d5 2018-02-12 stsp free(hex);
224 d1cda826 2017-11-06 stsp free(path_objects);
225 d1cda826 2017-11-06 stsp return err;
226 d1cda826 2017-11-06 stsp }
227 d1cda826 2017-11-06 stsp
228 4ee4114f 2018-01-23 stsp static const struct got_error *
229 eb651edf 2018-02-11 stsp open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
230 d1cda826 2017-11-06 stsp {
231 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
232 a1fd68d8 2018-01-12 stsp char *path;
233 6c00b545 2018-01-17 stsp
234 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
235 d1cda826 2017-11-06 stsp if (err)
236 d1cda826 2017-11-06 stsp return err;
237 4558fcd4 2018-01-14 stsp *f = fopen(path, "rb");
238 4558fcd4 2018-01-14 stsp if (*f == NULL) {
239 6c00b545 2018-01-17 stsp err = got_error_from_errno();
240 6c00b545 2018-01-17 stsp goto done;
241 a1fd68d8 2018-01-12 stsp }
242 4558fcd4 2018-01-14 stsp done:
243 4558fcd4 2018-01-14 stsp free(path);
244 4558fcd4 2018-01-14 stsp return err;
245 4558fcd4 2018-01-14 stsp }
246 a1fd68d8 2018-01-12 stsp
247 4558fcd4 2018-01-14 stsp const struct got_error *
248 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
249 4558fcd4 2018-01-14 stsp struct got_object_id *id)
250 4558fcd4 2018-01-14 stsp {
251 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
252 6c00b545 2018-01-17 stsp char *path;
253 4558fcd4 2018-01-14 stsp FILE *f;
254 4558fcd4 2018-01-14 stsp
255 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
256 4558fcd4 2018-01-14 stsp if (err)
257 4558fcd4 2018-01-14 stsp return err;
258 4558fcd4 2018-01-14 stsp
259 6c00b545 2018-01-17 stsp f = fopen(path, "rb");
260 6c00b545 2018-01-17 stsp if (f == NULL) {
261 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
262 6c00b545 2018-01-17 stsp err = got_error_from_errno();
263 6c00b545 2018-01-17 stsp goto done;
264 6c00b545 2018-01-17 stsp }
265 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
266 6c00b545 2018-01-17 stsp if (err)
267 6c00b545 2018-01-17 stsp goto done;
268 6c00b545 2018-01-17 stsp if (*obj == NULL)
269 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
270 6c00b545 2018-01-17 stsp } else {
271 6c00b545 2018-01-17 stsp err = read_object_header(obj, repo, f);
272 6c00b545 2018-01-17 stsp if (err)
273 6c00b545 2018-01-17 stsp goto done;
274 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
275 6c00b545 2018-01-17 stsp }
276 6c00b545 2018-01-17 stsp done:
277 6c00b545 2018-01-17 stsp free(path);
278 18e99ab6 2018-03-17 stsp if (f)
279 a1fd68d8 2018-01-12 stsp fclose(f);
280 ab9a70b2 2017-11-06 stsp return err;
281 6c00b545 2018-01-17 stsp
282 ab9a70b2 2017-11-06 stsp }
283 ab9a70b2 2017-11-06 stsp
284 6dfa2fd3 2018-02-12 stsp const struct got_error *
285 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
286 6dfa2fd3 2018-02-12 stsp const char *id_str)
287 6dfa2fd3 2018-02-12 stsp {
288 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
289 6dfa2fd3 2018-02-12 stsp
290 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
291 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
292 6dfa2fd3 2018-02-12 stsp
293 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
294 6dfa2fd3 2018-02-12 stsp }
295 6dfa2fd3 2018-02-12 stsp
296 ab9a70b2 2017-11-06 stsp void
297 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
298 ab9a70b2 2017-11-06 stsp {
299 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
300 c3703302 2018-01-23 stsp struct got_delta *delta;
301 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
302 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
303 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
304 c3703302 2018-01-23 stsp got_delta_close(delta);
305 96f5e8b3 2018-01-23 stsp }
306 96f5e8b3 2018-01-23 stsp }
307 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
308 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
309 ab9a70b2 2017-11-06 stsp free(obj);
310 d1cda826 2017-11-06 stsp }
311 d1cda826 2017-11-06 stsp
312 d1cda826 2017-11-06 stsp static const struct got_error *
313 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
314 d1cda826 2017-11-06 stsp {
315 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
316 d1cda826 2017-11-06 stsp char *s = buf;
317 d1cda826 2017-11-06 stsp size_t tlen;
318 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
319 d1cda826 2017-11-06 stsp
320 d1cda826 2017-11-06 stsp *commit = calloc(1, sizeof(**commit));
321 d1cda826 2017-11-06 stsp if (*commit == NULL)
322 0a585a0d 2018-03-17 stsp return got_error_from_errno();
323 59ece79d 2018-02-12 stsp (*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
324 59ece79d 2018-02-12 stsp if ((*commit)->tree_id == NULL) {
325 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
326 59ece79d 2018-02-12 stsp free(*commit);
327 59ece79d 2018-02-12 stsp *commit = NULL;
328 0a585a0d 2018-03-17 stsp return err;
329 59ece79d 2018-02-12 stsp }
330 d1cda826 2017-11-06 stsp
331 d1cda826 2017-11-06 stsp SIMPLEQ_INIT(&(*commit)->parent_ids);
332 d1cda826 2017-11-06 stsp
333 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
334 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
335 d1cda826 2017-11-06 stsp remain -= tlen;
336 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
337 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
338 d1cda826 2017-11-06 stsp goto done;
339 d1cda826 2017-11-06 stsp }
340 d1cda826 2017-11-06 stsp s += tlen;
341 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
342 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
343 d1cda826 2017-11-06 stsp goto done;
344 d1cda826 2017-11-06 stsp }
345 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
346 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
347 d1cda826 2017-11-06 stsp } else {
348 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 d1cda826 2017-11-06 stsp goto done;
350 d1cda826 2017-11-06 stsp }
351 d1cda826 2017-11-06 stsp
352 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
353 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
354 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
355 d1cda826 2017-11-06 stsp
356 d1cda826 2017-11-06 stsp remain -= tlen;
357 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
358 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
359 d1cda826 2017-11-06 stsp goto done;
360 ef0981d5 2018-02-12 stsp }
361 d1cda826 2017-11-06 stsp
362 d1cda826 2017-11-06 stsp pid = calloc(1, sizeof(*pid));
363 d1cda826 2017-11-06 stsp if (pid == NULL) {
364 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
365 d1cda826 2017-11-06 stsp goto done;
366 d1cda826 2017-11-06 stsp }
367 59ece79d 2018-02-12 stsp pid->id = calloc(1, sizeof(*pid->id));
368 59ece79d 2018-02-12 stsp if (pid->id == NULL) {
369 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
370 59ece79d 2018-02-12 stsp free(pid);
371 59ece79d 2018-02-12 stsp goto done;
372 59ece79d 2018-02-12 stsp }
373 59ece79d 2018-02-12 stsp s += tlen;
374 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest(pid->id->sha1, s)) {
375 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
376 59ece79d 2018-02-12 stsp free(pid->id);
377 59ece79d 2018-02-12 stsp free(pid);
378 d1cda826 2017-11-06 stsp goto done;
379 d1cda826 2017-11-06 stsp }
380 d1cda826 2017-11-06 stsp SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
381 d1cda826 2017-11-06 stsp (*commit)->nparents++;
382 d1cda826 2017-11-06 stsp
383 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
384 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
385 d1cda826 2017-11-06 stsp }
386 d1cda826 2017-11-06 stsp
387 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
388 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
389 d1cda826 2017-11-06 stsp char *p;
390 d1cda826 2017-11-06 stsp
391 d1cda826 2017-11-06 stsp remain -= tlen;
392 d1cda826 2017-11-06 stsp if (remain <= 0) {
393 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
394 d1cda826 2017-11-06 stsp goto done;
395 d1cda826 2017-11-06 stsp }
396 d1cda826 2017-11-06 stsp s += tlen;
397 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
398 d1cda826 2017-11-06 stsp if (p == NULL) {
399 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
400 d1cda826 2017-11-06 stsp goto done;
401 d1cda826 2017-11-06 stsp }
402 d1cda826 2017-11-06 stsp *p = '\0';
403 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
404 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
405 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
406 d1cda826 2017-11-06 stsp goto done;
407 d1cda826 2017-11-06 stsp }
408 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
409 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
410 d1cda826 2017-11-06 stsp }
411 d1cda826 2017-11-06 stsp
412 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
413 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
414 d1cda826 2017-11-06 stsp char *p;
415 d1cda826 2017-11-06 stsp
416 d1cda826 2017-11-06 stsp remain -= tlen;
417 d1cda826 2017-11-06 stsp if (remain <= 0) {
418 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
419 d1cda826 2017-11-06 stsp goto done;
420 d1cda826 2017-11-06 stsp }
421 d1cda826 2017-11-06 stsp s += tlen;
422 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
423 d1cda826 2017-11-06 stsp if (p == NULL) {
424 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
425 d1cda826 2017-11-06 stsp goto done;
426 d1cda826 2017-11-06 stsp }
427 d1cda826 2017-11-06 stsp *p = '\0';
428 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
429 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
430 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
431 d1cda826 2017-11-06 stsp goto done;
432 d1cda826 2017-11-06 stsp }
433 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
434 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
435 d1cda826 2017-11-06 stsp }
436 d1cda826 2017-11-06 stsp
437 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
438 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
439 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
440 eb651edf 2018-02-11 stsp goto done;
441 eb651edf 2018-02-11 stsp }
442 d1cda826 2017-11-06 stsp done:
443 59ece79d 2018-02-12 stsp if (err) {
444 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
445 59ece79d 2018-02-12 stsp *commit = NULL;
446 59ece79d 2018-02-12 stsp }
447 0ffeb3c2 2017-11-26 stsp return err;
448 0ffeb3c2 2017-11-26 stsp }
449 0ffeb3c2 2017-11-26 stsp
450 0ffeb3c2 2017-11-26 stsp static void
451 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
452 0ffeb3c2 2017-11-26 stsp {
453 59ece79d 2018-02-12 stsp free(te->id);
454 0ffeb3c2 2017-11-26 stsp free(te->name);
455 0ffeb3c2 2017-11-26 stsp free(te);
456 0ffeb3c2 2017-11-26 stsp }
457 0ffeb3c2 2017-11-26 stsp
458 0ffeb3c2 2017-11-26 stsp static const struct got_error *
459 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
460 0ffeb3c2 2017-11-26 stsp size_t maxlen)
461 0ffeb3c2 2017-11-26 stsp {
462 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
463 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
464 0ffeb3c2 2017-11-26 stsp
465 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
466 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
467 0a585a0d 2018-03-17 stsp return got_error_from_errno();
468 59ece79d 2018-02-12 stsp
469 59ece79d 2018-02-12 stsp (*te)->id = calloc(1, sizeof(*(*te)->id));
470 59ece79d 2018-02-12 stsp if ((*te)->id == NULL) {
471 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
472 59ece79d 2018-02-12 stsp free(*te);
473 59ece79d 2018-02-12 stsp *te = NULL;
474 0a585a0d 2018-03-17 stsp return err;
475 59ece79d 2018-02-12 stsp }
476 0ffeb3c2 2017-11-26 stsp
477 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
478 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
479 0ffeb3c2 2017-11-26 stsp free(*te);
480 59ece79d 2018-02-12 stsp *te = NULL;
481 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
482 0ffeb3c2 2017-11-26 stsp }
483 0ffeb3c2 2017-11-26 stsp
484 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
485 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
486 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 0ffeb3c2 2017-11-26 stsp free(*te);
488 59ece79d 2018-02-12 stsp *te = NULL;
489 0a585a0d 2018-03-17 stsp return err;
490 0ffeb3c2 2017-11-26 stsp }
491 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
492 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
493 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 0ffeb3c2 2017-11-26 stsp goto done;
495 0ffeb3c2 2017-11-26 stsp }
496 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
497 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
498 0ffeb3c2 2017-11-26 stsp p++;
499 0ffeb3c2 2017-11-26 stsp }
500 0ffeb3c2 2017-11-26 stsp
501 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
502 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
503 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 0ffeb3c2 2017-11-26 stsp goto done;
505 0ffeb3c2 2017-11-26 stsp }
506 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
507 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
508 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
509 0ffeb3c2 2017-11-26 stsp done:
510 59ece79d 2018-02-12 stsp if (err) {
511 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
512 59ece79d 2018-02-12 stsp *te = NULL;
513 59ece79d 2018-02-12 stsp }
514 d1cda826 2017-11-06 stsp return err;
515 d1cda826 2017-11-06 stsp }
516 d1cda826 2017-11-06 stsp
517 d1cda826 2017-11-06 stsp static const struct got_error *
518 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
519 e0ab43e7 2018-03-16 stsp uint8_t *buf, size_t len)
520 0ffeb3c2 2017-11-26 stsp {
521 90356acc 2018-01-27 stsp const struct got_error *err;
522 0ffeb3c2 2017-11-26 stsp size_t remain = len;
523 0ffeb3c2 2017-11-26 stsp
524 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
525 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
526 0a585a0d 2018-03-17 stsp return got_error_from_errno();
527 0ffeb3c2 2017-11-26 stsp
528 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
529 0ffeb3c2 2017-11-26 stsp
530 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
531 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
532 0ffeb3c2 2017-11-26 stsp size_t elen;
533 0ffeb3c2 2017-11-26 stsp
534 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
535 90356acc 2018-01-27 stsp if (err)
536 90356acc 2018-01-27 stsp return err;
537 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
538 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
539 0ffeb3c2 2017-11-26 stsp buf += elen;
540 0ffeb3c2 2017-11-26 stsp remain -= elen;
541 0ffeb3c2 2017-11-26 stsp }
542 0ffeb3c2 2017-11-26 stsp
543 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
544 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
545 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
546 0ffeb3c2 2017-11-26 stsp }
547 0ffeb3c2 2017-11-26 stsp
548 0ffeb3c2 2017-11-26 stsp return NULL;
549 0ffeb3c2 2017-11-26 stsp }
550 0ffeb3c2 2017-11-26 stsp
551 0ffeb3c2 2017-11-26 stsp static const struct got_error *
552 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
553 eb651edf 2018-02-11 stsp {
554 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
555 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
556 eb651edf 2018-02-11 stsp size_t n, total, remain;
557 eb651edf 2018-02-11 stsp uint8_t *buf;
558 eb651edf 2018-02-11 stsp
559 eb651edf 2018-02-11 stsp *outbuf = NULL;
560 eb651edf 2018-02-11 stsp *outlen = 0;
561 eb651edf 2018-02-11 stsp
562 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
563 eb651edf 2018-02-11 stsp if (buf == NULL)
564 0a585a0d 2018-03-17 stsp return got_error_from_errno();
565 eb651edf 2018-02-11 stsp
566 eb651edf 2018-02-11 stsp remain = blocksize;
567 eb651edf 2018-02-11 stsp total = 0;
568 eb651edf 2018-02-11 stsp while (1) {
569 eb651edf 2018-02-11 stsp if (remain == 0) {
570 eb651edf 2018-02-11 stsp uint8_t *newbuf;
571 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
572 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
573 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
574 eb651edf 2018-02-11 stsp goto done;
575 eb651edf 2018-02-11 stsp }
576 eb651edf 2018-02-11 stsp buf = newbuf;
577 eb651edf 2018-02-11 stsp remain += blocksize;
578 eb651edf 2018-02-11 stsp }
579 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
580 eb651edf 2018-02-11 stsp if (n == 0) {
581 eb651edf 2018-02-11 stsp if (ferror(f)) {
582 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
583 eb651edf 2018-02-11 stsp goto done;
584 eb651edf 2018-02-11 stsp }
585 eb651edf 2018-02-11 stsp break; /* EOF */
586 eb651edf 2018-02-11 stsp }
587 eb651edf 2018-02-11 stsp remain -= n;
588 eb651edf 2018-02-11 stsp total += n;
589 eb651edf 2018-02-11 stsp };
590 eb651edf 2018-02-11 stsp
591 eb651edf 2018-02-11 stsp done:
592 eb651edf 2018-02-11 stsp if (err == NULL) {
593 eb651edf 2018-02-11 stsp *outbuf = buf;
594 eb651edf 2018-02-11 stsp *outlen = total;
595 eb651edf 2018-02-11 stsp } else
596 eb651edf 2018-02-11 stsp free(buf);
597 eb651edf 2018-02-11 stsp return err;
598 eb651edf 2018-02-11 stsp }
599 eb651edf 2018-02-11 stsp
600 eb651edf 2018-02-11 stsp static const struct got_error *
601 d1cda826 2017-11-06 stsp read_commit_object(struct got_commit_object **commit,
602 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
603 d1cda826 2017-11-06 stsp {
604 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
605 d1cda826 2017-11-06 stsp size_t len;
606 eb651edf 2018-02-11 stsp uint8_t *p;
607 d1cda826 2017-11-06 stsp
608 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
609 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
610 eb651edf 2018-02-11 stsp else
611 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
612 4558fcd4 2018-01-14 stsp if (err)
613 d1cda826 2017-11-06 stsp return err;
614 d1cda826 2017-11-06 stsp
615 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
616 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
617 d1cda826 2017-11-06 stsp goto done;
618 d1cda826 2017-11-06 stsp }
619 d1cda826 2017-11-06 stsp
620 d1cda826 2017-11-06 stsp /* Skip object header. */
621 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
622 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
623 eb651edf 2018-02-11 stsp free(p);
624 d1cda826 2017-11-06 stsp done:
625 d1cda826 2017-11-06 stsp return err;
626 d1cda826 2017-11-06 stsp }
627 d1cda826 2017-11-06 stsp
628 d1cda826 2017-11-06 stsp const struct got_error *
629 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
630 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
631 d1cda826 2017-11-06 stsp {
632 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
633 d1cda826 2017-11-06 stsp
634 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
635 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
636 d1cda826 2017-11-06 stsp
637 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
638 ea35256b 2018-03-16 stsp uint8_t *buf;
639 ea35256b 2018-03-16 stsp size_t len;
640 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
641 ea35256b 2018-03-16 stsp if (err)
642 ea35256b 2018-03-16 stsp return err;
643 b29656e2 2018-03-16 stsp obj->size = len;
644 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
645 ea35256b 2018-03-16 stsp free(buf);
646 ea35256b 2018-03-16 stsp } else {
647 ea35256b 2018-03-16 stsp FILE *f;
648 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
649 ea35256b 2018-03-16 stsp if (err)
650 ea35256b 2018-03-16 stsp return err;
651 ea35256b 2018-03-16 stsp err = read_commit_object(commit, repo, obj, f);
652 ea35256b 2018-03-16 stsp fclose(f);
653 ea35256b 2018-03-16 stsp }
654 d1cda826 2017-11-06 stsp return err;
655 d1cda826 2017-11-06 stsp }
656 d1cda826 2017-11-06 stsp
657 d1cda826 2017-11-06 stsp void
658 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
659 d1cda826 2017-11-06 stsp {
660 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
661 d1cda826 2017-11-06 stsp
662 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
663 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
664 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
665 59ece79d 2018-02-12 stsp free(pid->id);
666 d1cda826 2017-11-06 stsp free(pid);
667 d1cda826 2017-11-06 stsp }
668 d1cda826 2017-11-06 stsp
669 59ece79d 2018-02-12 stsp free(commit->tree_id);
670 d1cda826 2017-11-06 stsp free(commit->author);
671 d1cda826 2017-11-06 stsp free(commit->committer);
672 d1cda826 2017-11-06 stsp free(commit->logmsg);
673 d1cda826 2017-11-06 stsp free(commit);
674 d1cda826 2017-11-06 stsp }
675 0ffeb3c2 2017-11-26 stsp
676 0ffeb3c2 2017-11-26 stsp static const struct got_error *
677 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
678 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
679 0ffeb3c2 2017-11-26 stsp {
680 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
681 0ffeb3c2 2017-11-26 stsp size_t len;
682 eb651edf 2018-02-11 stsp uint8_t *p;
683 0ffeb3c2 2017-11-26 stsp
684 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
685 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
686 eb651edf 2018-02-11 stsp else
687 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
688 4558fcd4 2018-01-14 stsp if (err)
689 0ffeb3c2 2017-11-26 stsp return err;
690 0ffeb3c2 2017-11-26 stsp
691 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
692 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
693 0ffeb3c2 2017-11-26 stsp goto done;
694 0ffeb3c2 2017-11-26 stsp }
695 0ffeb3c2 2017-11-26 stsp
696 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
697 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
698 eb651edf 2018-02-11 stsp err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
699 eb651edf 2018-02-11 stsp free(p);
700 0ffeb3c2 2017-11-26 stsp done:
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 const struct got_error *
705 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
706 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
707 0ffeb3c2 2017-11-26 stsp {
708 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
709 0ffeb3c2 2017-11-26 stsp
710 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
711 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
712 0ffeb3c2 2017-11-26 stsp
713 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
714 e0ab43e7 2018-03-16 stsp uint8_t *buf;
715 e0ab43e7 2018-03-16 stsp size_t len;
716 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
717 e0ab43e7 2018-03-16 stsp if (err)
718 e0ab43e7 2018-03-16 stsp return err;
719 b29656e2 2018-03-16 stsp obj->size = len;
720 b29656e2 2018-03-16 stsp err = parse_tree_object(tree, repo, buf, len);
721 e0ab43e7 2018-03-16 stsp free(buf);
722 e0ab43e7 2018-03-16 stsp } else {
723 e0ab43e7 2018-03-16 stsp FILE *f;
724 eb651edf 2018-02-11 stsp err = open_loose_object(&f, obj, repo);
725 e0ab43e7 2018-03-16 stsp if (err)
726 e0ab43e7 2018-03-16 stsp return err;
727 e0ab43e7 2018-03-16 stsp err = read_tree_object(tree, repo, obj, f);
728 e0ab43e7 2018-03-16 stsp fclose(f);
729 e0ab43e7 2018-03-16 stsp }
730 0ffeb3c2 2017-11-26 stsp return err;
731 0ffeb3c2 2017-11-26 stsp }
732 0ffeb3c2 2017-11-26 stsp
733 0ffeb3c2 2017-11-26 stsp void
734 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
735 0ffeb3c2 2017-11-26 stsp {
736 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
737 f715ca7f 2017-11-27 stsp
738 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
739 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
740 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
741 f715ca7f 2017-11-27 stsp tree_entry_close(te);
742 f715ca7f 2017-11-27 stsp }
743 f715ca7f 2017-11-27 stsp
744 f715ca7f 2017-11-27 stsp free(tree);
745 68482ea3 2017-11-27 stsp }
746 68482ea3 2017-11-27 stsp
747 68482ea3 2017-11-27 stsp const struct got_error *
748 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
749 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
750 68482ea3 2017-11-27 stsp {
751 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
752 68482ea3 2017-11-27 stsp
753 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
754 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
755 68482ea3 2017-11-27 stsp
756 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
757 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
758 7d283eee 2017-11-29 stsp
759 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
760 4558fcd4 2018-01-14 stsp if (*blob == NULL)
761 0a585a0d 2018-03-17 stsp return got_error_from_errno();
762 68482ea3 2017-11-27 stsp
763 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
764 eb651edf 2018-02-11 stsp (*blob)->read_buf = calloc(1, blocksize);
765 56866f4a 2018-03-17 stsp if ((*blob)->read_buf == NULL) {
766 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
767 56866f4a 2018-03-17 stsp free(*blob);
768 56866f4a 2018-03-17 stsp *blob = NULL;
769 0a585a0d 2018-03-17 stsp return err;
770 56866f4a 2018-03-17 stsp }
771 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
772 56866f4a 2018-03-17 stsp if (err) {
773 56866f4a 2018-03-17 stsp free((*blob)->read_buf);
774 56866f4a 2018-03-17 stsp free(*blob);
775 56866f4a 2018-03-17 stsp *blob = NULL;
776 eb651edf 2018-02-11 stsp return err;
777 56866f4a 2018-03-17 stsp }
778 eb651edf 2018-02-11 stsp } else {
779 eb651edf 2018-02-11 stsp err = open_loose_object(&((*blob)->f), obj, repo);
780 eb651edf 2018-02-11 stsp if (err) {
781 eb651edf 2018-02-11 stsp free(*blob);
782 d0f3be7c 2018-03-17 stsp *blob = NULL;
783 eb651edf 2018-02-11 stsp return err;
784 eb651edf 2018-02-11 stsp }
785 68482ea3 2017-11-27 stsp
786 19d747f7 2018-03-16 stsp err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
787 eb651edf 2018-02-11 stsp if (err != NULL) {
788 eb651edf 2018-02-11 stsp fclose((*blob)->f);
789 eb651edf 2018-02-11 stsp free(*blob);
790 56866f4a 2018-03-17 stsp *blob = NULL;
791 eb651edf 2018-02-11 stsp return err;
792 eb651edf 2018-02-11 stsp }
793 eb651edf 2018-02-11 stsp
794 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
795 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
796 68482ea3 2017-11-27 stsp }
797 68482ea3 2017-11-27 stsp
798 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
799 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
800 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
801 7d283eee 2017-11-29 stsp
802 68482ea3 2017-11-27 stsp return err;
803 0ffeb3c2 2017-11-26 stsp }
804 68482ea3 2017-11-27 stsp
805 68482ea3 2017-11-27 stsp void
806 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
807 68482ea3 2017-11-27 stsp {
808 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
809 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
810 eb651edf 2018-02-11 stsp else
811 eb651edf 2018-02-11 stsp free(blob->read_buf);
812 68482ea3 2017-11-27 stsp fclose(blob->f);
813 68482ea3 2017-11-27 stsp free(blob);
814 f934cf2c 2018-02-12 stsp }
815 f934cf2c 2018-02-12 stsp
816 f934cf2c 2018-02-12 stsp char *
817 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
818 f934cf2c 2018-02-12 stsp {
819 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
820 f934cf2c 2018-02-12 stsp }
821 f934cf2c 2018-02-12 stsp
822 f934cf2c 2018-02-12 stsp size_t
823 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
824 f934cf2c 2018-02-12 stsp {
825 f934cf2c 2018-02-12 stsp return blob->hdrlen;
826 68482ea3 2017-11-27 stsp }
827 68482ea3 2017-11-27 stsp
828 f934cf2c 2018-02-12 stsp const uint8_t *
829 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
830 f934cf2c 2018-02-12 stsp {
831 f934cf2c 2018-02-12 stsp return blob->read_buf;
832 f934cf2c 2018-02-12 stsp }
833 f934cf2c 2018-02-12 stsp
834 68482ea3 2017-11-27 stsp const struct got_error *
835 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
836 68482ea3 2017-11-27 stsp {
837 eb651edf 2018-02-11 stsp size_t n;
838 eb651edf 2018-02-11 stsp
839 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
840 eb651edf 2018-02-11 stsp return got_inflate_read(&blob->zb, blob->f, outlenp);
841 eb651edf 2018-02-11 stsp
842 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
843 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
844 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
845 eb651edf 2018-02-11 stsp *outlenp = n;
846 eb651edf 2018-02-11 stsp return NULL;
847 68482ea3 2017-11-27 stsp }