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