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