2 a440fac0 2018-09-06 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 a440fac0 2018-09-06 stsp * Permission to use, copy, modify, and distribute this software for any
5 a440fac0 2018-09-06 stsp * purpose with or without fee is hereby granted, provided that the above
6 a440fac0 2018-09-06 stsp * copyright notice and this permission notice appear in all copies.
8 a440fac0 2018-09-06 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a440fac0 2018-09-06 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a440fac0 2018-09-06 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a440fac0 2018-09-06 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a440fac0 2018-09-06 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a440fac0 2018-09-06 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a440fac0 2018-09-06 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 a440fac0 2018-09-06 stsp #include <sys/types.h>
18 a440fac0 2018-09-06 stsp #include <sys/stat.h>
19 a440fac0 2018-09-06 stsp #include <sys/queue.h>
20 a440fac0 2018-09-06 stsp #include <sys/uio.h>
21 a440fac0 2018-09-06 stsp #include <sys/socket.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
25 a440fac0 2018-09-06 stsp #include <errno.h>
26 a440fac0 2018-09-06 stsp #include <stdio.h>
27 a440fac0 2018-09-06 stsp #include <stdlib.h>
28 a440fac0 2018-09-06 stsp #include <string.h>
29 a440fac0 2018-09-06 stsp #include <stdint.h>
30 a440fac0 2018-09-06 stsp #include <sha1.h>
31 a440fac0 2018-09-06 stsp #include <zlib.h>
32 a440fac0 2018-09-06 stsp #include <ctype.h>
33 a440fac0 2018-09-06 stsp #include <limits.h>
34 a440fac0 2018-09-06 stsp #include <imsg.h>
35 a440fac0 2018-09-06 stsp #include <time.h>
36 ad242220 2018-09-08 stsp #include <unistd.h>
38 a440fac0 2018-09-06 stsp #include "got_error.h"
39 a440fac0 2018-09-06 stsp #include "got_object.h"
40 a440fac0 2018-09-06 stsp #include "got_repository.h"
41 a440fac0 2018-09-06 stsp #include "got_opentemp.h"
43 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
45 7762fe12 2018-11-05 stsp #include "got_lib_privsep.h"
46 7762fe12 2018-11-05 stsp #include "got_lib_pack.h"
47 41fa1437 2018-11-05 stsp #include "got_lib_inflate.h"
48 41fa1437 2018-11-05 stsp #include "got_lib_object.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
50 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
52 a440fac0 2018-09-06 stsp #ifndef nitems
53 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_TREE "tree"
58 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
60 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
61 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
62 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
63 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
66 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
67 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
69 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
72 2ff12563 2018-09-15 stsp const struct got_error *
73 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
75 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
77 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
78 5df4932d 2018-11-05 stsp if (*qid == NULL)
79 5df4932d 2018-11-05 stsp return got_error_from_errno();
81 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
82 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
83 5df4932d 2018-11-05 stsp err = got_error_from_errno();
84 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
85 5df4932d 2018-11-05 stsp *qid = NULL;
89 5df4932d 2018-11-05 stsp return NULL;
92 5df4932d 2018-11-05 stsp const struct got_error *
93 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
95 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
97 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
98 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
99 2ff12563 2018-09-15 stsp return got_error_from_errno();
101 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
102 2ff12563 2018-09-15 stsp free(*outbuf);
103 2ff12563 2018-09-15 stsp *outbuf = NULL;
104 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
107 2ff12563 2018-09-15 stsp return NULL;
111 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
113 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
114 03fa71c8 2018-09-06 stsp obj->refcnt--;
115 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
119 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
120 03fa71c8 2018-09-06 stsp struct got_delta *delta;
121 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
122 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
123 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
124 03fa71c8 2018-09-06 stsp got_delta_close(delta);
127 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
128 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
133 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
135 03fa71c8 2018-09-06 stsp free(qid->id);
139 a440fac0 2018-09-06 stsp struct got_commit_object *
140 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
142 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
144 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
145 a440fac0 2018-09-06 stsp if (commit == NULL)
146 a440fac0 2018-09-06 stsp return NULL;
147 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
148 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
149 a440fac0 2018-09-06 stsp free(commit);
150 a440fac0 2018-09-06 stsp return NULL;
153 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
155 a440fac0 2018-09-06 stsp return commit;
158 a440fac0 2018-09-06 stsp const struct got_error *
159 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
160 a440fac0 2018-09-06 stsp const char *id_str)
162 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
163 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
165 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
167 7762fe12 2018-11-05 stsp return err;
169 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
170 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
171 a440fac0 2018-09-06 stsp free(qid->id);
173 a440fac0 2018-09-06 stsp return err;
176 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
177 a440fac0 2018-09-06 stsp commit->nparents++;
179 a440fac0 2018-09-06 stsp return NULL;
182 a440fac0 2018-09-06 stsp static const struct got_error *
183 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
185 a440fac0 2018-09-06 stsp int sign = 1;
186 a440fac0 2018-09-06 stsp const char *p = tzstr;
187 a440fac0 2018-09-06 stsp time_t h, m;
189 a440fac0 2018-09-06 stsp *gmtoff = 0;
191 a440fac0 2018-09-06 stsp if (*p == '-')
193 a440fac0 2018-09-06 stsp else if (*p != '+')
194 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
196 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
197 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
198 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
201 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
202 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
203 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
205 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
206 a440fac0 2018-09-06 stsp return NULL;
209 a440fac0 2018-09-06 stsp static const struct got_error *
210 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
212 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
213 a440fac0 2018-09-06 stsp const char *errstr;
214 a440fac0 2018-09-06 stsp char *space, *tzstr;
216 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
217 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
218 a440fac0 2018-09-06 stsp if (space == NULL)
219 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
220 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
221 a440fac0 2018-09-06 stsp if (tzstr == NULL)
222 a440fac0 2018-09-06 stsp return got_error_from_errno();
223 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
224 a440fac0 2018-09-06 stsp free(tzstr);
226 a440fac0 2018-09-06 stsp return err;
227 a440fac0 2018-09-06 stsp *space = '\0';
229 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
230 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
231 a440fac0 2018-09-06 stsp if (space == NULL)
232 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
234 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
235 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
236 a440fac0 2018-09-06 stsp if (errstr)
237 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
239 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
240 ccb26ccd 2018-11-05 stsp *time -= *gmtoff;
242 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
243 a440fac0 2018-09-06 stsp *space = '\0';
245 a440fac0 2018-09-06 stsp return NULL;
249 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
251 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
253 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
254 03fa71c8 2018-09-06 stsp commit->refcnt--;
255 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
259 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
260 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
261 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
262 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
265 03fa71c8 2018-09-06 stsp free(commit->tree_id);
266 03fa71c8 2018-09-06 stsp free(commit->author);
267 03fa71c8 2018-09-06 stsp free(commit->committer);
268 03fa71c8 2018-09-06 stsp free(commit->logmsg);
269 03fa71c8 2018-09-06 stsp free(commit);
272 a440fac0 2018-09-06 stsp const struct got_error *
273 41fa1437 2018-11-05 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
275 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
276 a440fac0 2018-09-06 stsp char *s = buf;
277 a440fac0 2018-09-06 stsp size_t tlen;
278 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
280 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
281 a440fac0 2018-09-06 stsp if (*commit == NULL)
282 a440fac0 2018-09-06 stsp return got_error_from_errno();
284 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
285 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
286 a440fac0 2018-09-06 stsp remain -= tlen;
287 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
288 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
292 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
293 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
296 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
297 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
299 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
303 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
304 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
305 a440fac0 2018-09-06 stsp remain -= tlen;
306 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
307 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
311 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
315 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
316 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
319 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
320 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
322 a440fac0 2018-09-06 stsp size_t slen;
324 a440fac0 2018-09-06 stsp remain -= tlen;
325 a440fac0 2018-09-06 stsp if (remain <= 0) {
326 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
330 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
331 a440fac0 2018-09-06 stsp if (p == NULL) {
332 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 a440fac0 2018-09-06 stsp slen = strlen(s);
337 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
338 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
341 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
342 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
343 a440fac0 2018-09-06 stsp err = got_error_from_errno();
346 a440fac0 2018-09-06 stsp s += slen + 1;
347 a440fac0 2018-09-06 stsp remain -= slen + 1;
350 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
351 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
353 a440fac0 2018-09-06 stsp size_t slen;
355 a440fac0 2018-09-06 stsp remain -= tlen;
356 a440fac0 2018-09-06 stsp if (remain <= 0) {
357 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
361 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
362 a440fac0 2018-09-06 stsp if (p == NULL) {
363 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
367 a440fac0 2018-09-06 stsp slen = strlen(s);
368 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
369 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
372 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
373 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
374 a440fac0 2018-09-06 stsp err = got_error_from_errno();
377 a440fac0 2018-09-06 stsp s += slen + 1;
378 a440fac0 2018-09-06 stsp remain -= slen + 1;
381 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
382 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
383 a440fac0 2018-09-06 stsp err = got_error_from_errno();
388 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
389 a440fac0 2018-09-06 stsp *commit = NULL;
391 a440fac0 2018-09-06 stsp return err;
395 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
397 a440fac0 2018-09-06 stsp free(te->id);
398 a440fac0 2018-09-06 stsp free(te->name);
403 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
405 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
407 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
408 03fa71c8 2018-09-06 stsp tree->refcnt--;
409 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
413 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
414 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
415 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
416 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
419 03fa71c8 2018-09-06 stsp free(tree);
422 a440fac0 2018-09-06 stsp struct got_tree_entry *
423 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
425 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
427 32ac459c 2018-11-05 stsp te = malloc(sizeof(*te));
428 a440fac0 2018-09-06 stsp if (te == NULL)
429 a440fac0 2018-09-06 stsp return NULL;
431 32ac459c 2018-11-05 stsp te->id = malloc(sizeof(*te->id));
432 a440fac0 2018-09-06 stsp if (te->id == NULL) {
439 a440fac0 2018-09-06 stsp static const struct got_error *
440 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
441 a440fac0 2018-09-06 stsp size_t maxlen)
443 a440fac0 2018-09-06 stsp char *p = buf, *space;
444 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
446 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
447 a440fac0 2018-09-06 stsp if (*te == NULL)
448 a440fac0 2018-09-06 stsp return got_error_from_errno();
450 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
451 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
453 a440fac0 2018-09-06 stsp *te = NULL;
454 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
457 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
458 a440fac0 2018-09-06 stsp if (space == NULL) {
459 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 a440fac0 2018-09-06 stsp *te = NULL;
462 a440fac0 2018-09-06 stsp return err;
464 6dfaee02 2018-11-05 stsp (*te)->mode = 0;
465 a440fac0 2018-09-06 stsp while (*p != ' ') {
466 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
467 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
470 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
471 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
475 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
476 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
477 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
480 68bf1b1e 2018-11-07 stsp buf += *elen;
481 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
482 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
485 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
486 a440fac0 2018-09-06 stsp *te = NULL;
488 a440fac0 2018-09-06 stsp return err;
491 a440fac0 2018-09-06 stsp const struct got_error *
492 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
494 a440fac0 2018-09-06 stsp const struct got_error *err;
495 a440fac0 2018-09-06 stsp size_t remain = len;
497 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
498 a440fac0 2018-09-06 stsp if (*tree == NULL)
499 a440fac0 2018-09-06 stsp return got_error_from_errno();
501 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
503 a440fac0 2018-09-06 stsp while (remain > 0) {
504 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
505 a440fac0 2018-09-06 stsp size_t elen;
507 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
509 a440fac0 2018-09-06 stsp return err;
510 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
511 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
512 a440fac0 2018-09-06 stsp buf += elen;
513 a440fac0 2018-09-06 stsp remain -= elen;
516 a440fac0 2018-09-06 stsp if (remain != 0) {
517 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
518 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
521 a440fac0 2018-09-06 stsp return NULL;
524 ad242220 2018-09-08 stsp const struct got_error *
525 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
527 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
528 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
529 a440fac0 2018-09-06 stsp size_t n, total, remain;
530 a440fac0 2018-09-06 stsp uint8_t *buf;
532 a440fac0 2018-09-06 stsp *outbuf = NULL;
533 a440fac0 2018-09-06 stsp *outlen = 0;
535 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
536 a440fac0 2018-09-06 stsp if (buf == NULL)
537 a440fac0 2018-09-06 stsp return got_error_from_errno();
539 a440fac0 2018-09-06 stsp remain = blocksize;
541 a440fac0 2018-09-06 stsp while (1) {
542 a440fac0 2018-09-06 stsp if (remain == 0) {
543 a440fac0 2018-09-06 stsp uint8_t *newbuf;
544 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
545 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
546 a440fac0 2018-09-06 stsp err = got_error_from_errno();
549 a440fac0 2018-09-06 stsp buf = newbuf;
550 a440fac0 2018-09-06 stsp remain += blocksize;
552 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
553 a440fac0 2018-09-06 stsp if (n == 0) {
554 a440fac0 2018-09-06 stsp if (ferror(f)) {
555 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
558 a440fac0 2018-09-06 stsp break; /* EOF */
560 a440fac0 2018-09-06 stsp remain -= n;
561 a440fac0 2018-09-06 stsp total += n;
565 a440fac0 2018-09-06 stsp if (err == NULL) {
566 a440fac0 2018-09-06 stsp *outbuf = buf;
567 a440fac0 2018-09-06 stsp *outlen = total;
570 ad242220 2018-09-08 stsp return err;