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