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 f4a881ce 2018-11-17 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 f4a881ce 2018-11-17 stsp #define GOT_OBJ_TAG_TREE "tree"
58 f4a881ce 2018-11-17 stsp #define GOT_OBJ_TAG_BLOB "blob"
59 f4a881ce 2018-11-17 stsp #define GOT_OBJ_TAG_TAG "tag"
61 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
62 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
63 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
64 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
66 f4a881ce 2018-11-17 stsp #define GOT_TAG_TAG_OBJECT "object "
67 f4a881ce 2018-11-17 stsp #define GOT_TAG_TAG_TYPE "type "
68 f4a881ce 2018-11-17 stsp #define GOT_TAG_TAG_TAG "tag "
69 f4a881ce 2018-11-17 stsp #define GOT_TAG_TAG_TAGGER "tagger "
72 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
73 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
75 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
78 2ff12563 2018-09-15 stsp const struct got_error *
79 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
81 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
83 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
84 5df4932d 2018-11-05 stsp if (*qid == NULL)
85 5df4932d 2018-11-05 stsp return got_error_from_errno();
87 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
88 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
89 5df4932d 2018-11-05 stsp err = got_error_from_errno();
90 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
91 5df4932d 2018-11-05 stsp *qid = NULL;
95 5df4932d 2018-11-05 stsp return NULL;
98 5df4932d 2018-11-05 stsp const struct got_error *
99 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
101 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
103 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
104 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
105 2ff12563 2018-09-15 stsp return got_error_from_errno();
107 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
108 2ff12563 2018-09-15 stsp free(*outbuf);
109 2ff12563 2018-09-15 stsp *outbuf = NULL;
110 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
113 2ff12563 2018-09-15 stsp return NULL;
117 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
119 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
120 03fa71c8 2018-09-06 stsp obj->refcnt--;
121 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
125 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
126 03fa71c8 2018-09-06 stsp struct got_delta *delta;
127 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
128 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
129 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
130 03fa71c8 2018-09-06 stsp got_delta_close(delta);
133 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
134 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
139 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
141 03fa71c8 2018-09-06 stsp free(qid->id);
145 a440fac0 2018-09-06 stsp struct got_commit_object *
146 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
148 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
150 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
151 a440fac0 2018-09-06 stsp if (commit == NULL)
152 a440fac0 2018-09-06 stsp return NULL;
153 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
154 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
155 a440fac0 2018-09-06 stsp free(commit);
156 a440fac0 2018-09-06 stsp return NULL;
159 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
161 a440fac0 2018-09-06 stsp return commit;
164 a440fac0 2018-09-06 stsp const struct got_error *
165 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
166 a440fac0 2018-09-06 stsp const char *id_str)
168 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
169 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
171 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
173 7762fe12 2018-11-05 stsp return err;
175 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
176 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
177 a440fac0 2018-09-06 stsp free(qid->id);
179 a440fac0 2018-09-06 stsp return err;
182 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
183 a440fac0 2018-09-06 stsp commit->nparents++;
185 a440fac0 2018-09-06 stsp return NULL;
188 a440fac0 2018-09-06 stsp static const struct got_error *
189 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
191 a440fac0 2018-09-06 stsp int sign = 1;
192 a440fac0 2018-09-06 stsp const char *p = tzstr;
193 a440fac0 2018-09-06 stsp time_t h, m;
195 a440fac0 2018-09-06 stsp *gmtoff = 0;
197 a440fac0 2018-09-06 stsp if (*p == '-')
199 a440fac0 2018-09-06 stsp else if (*p != '+')
200 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
202 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
203 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
204 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
207 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
208 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
209 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
211 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
212 a440fac0 2018-09-06 stsp return NULL;
215 a440fac0 2018-09-06 stsp static const struct got_error *
216 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
218 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
219 a440fac0 2018-09-06 stsp const char *errstr;
220 a440fac0 2018-09-06 stsp char *space, *tzstr;
222 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
223 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
224 a440fac0 2018-09-06 stsp if (space == NULL)
225 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
226 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
227 a440fac0 2018-09-06 stsp if (tzstr == NULL)
228 a440fac0 2018-09-06 stsp return got_error_from_errno();
229 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
230 a440fac0 2018-09-06 stsp free(tzstr);
232 a440fac0 2018-09-06 stsp return err;
233 a440fac0 2018-09-06 stsp *space = '\0';
235 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
236 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
237 a440fac0 2018-09-06 stsp if (space == NULL)
238 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
240 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
241 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
242 a440fac0 2018-09-06 stsp if (errstr)
243 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
245 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
246 ccb26ccd 2018-11-05 stsp *time -= *gmtoff;
248 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
249 a440fac0 2018-09-06 stsp *space = '\0';
251 a440fac0 2018-09-06 stsp return NULL;
255 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
257 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
259 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
260 03fa71c8 2018-09-06 stsp commit->refcnt--;
261 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
265 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
266 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
267 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
268 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
271 03fa71c8 2018-09-06 stsp free(commit->tree_id);
272 03fa71c8 2018-09-06 stsp free(commit->author);
273 03fa71c8 2018-09-06 stsp free(commit->committer);
274 03fa71c8 2018-09-06 stsp free(commit->logmsg);
275 03fa71c8 2018-09-06 stsp free(commit);
278 a440fac0 2018-09-06 stsp const struct got_error *
279 41fa1437 2018-11-05 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
281 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
282 a440fac0 2018-09-06 stsp char *s = buf;
283 a440fac0 2018-09-06 stsp size_t tlen;
284 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
286 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
287 a440fac0 2018-09-06 stsp if (*commit == NULL)
288 a440fac0 2018-09-06 stsp return got_error_from_errno();
290 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
291 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
292 a440fac0 2018-09-06 stsp remain -= tlen;
293 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
294 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
298 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
299 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
302 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
303 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
305 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
309 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
310 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
311 a440fac0 2018-09-06 stsp remain -= tlen;
312 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
313 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
317 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
321 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
322 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
325 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
326 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
328 a440fac0 2018-09-06 stsp size_t slen;
330 a440fac0 2018-09-06 stsp remain -= tlen;
331 a440fac0 2018-09-06 stsp if (remain <= 0) {
332 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
336 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
337 a440fac0 2018-09-06 stsp if (p == NULL) {
338 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
342 a440fac0 2018-09-06 stsp slen = strlen(s);
343 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
344 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
347 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
348 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
349 a440fac0 2018-09-06 stsp err = got_error_from_errno();
352 a440fac0 2018-09-06 stsp s += slen + 1;
353 a440fac0 2018-09-06 stsp remain -= slen + 1;
356 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
357 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
359 a440fac0 2018-09-06 stsp size_t slen;
361 a440fac0 2018-09-06 stsp remain -= tlen;
362 a440fac0 2018-09-06 stsp if (remain <= 0) {
363 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
367 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
368 a440fac0 2018-09-06 stsp if (p == NULL) {
369 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 a440fac0 2018-09-06 stsp slen = strlen(s);
374 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
375 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
378 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
379 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
380 a440fac0 2018-09-06 stsp err = got_error_from_errno();
383 a440fac0 2018-09-06 stsp s += slen + 1;
384 a440fac0 2018-09-06 stsp remain -= slen + 1;
387 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
388 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
389 a440fac0 2018-09-06 stsp err = got_error_from_errno();
394 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
395 a440fac0 2018-09-06 stsp *commit = NULL;
397 a440fac0 2018-09-06 stsp return err;
401 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
403 a440fac0 2018-09-06 stsp free(te->id);
404 a440fac0 2018-09-06 stsp free(te->name);
409 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
411 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
413 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
414 03fa71c8 2018-09-06 stsp tree->refcnt--;
415 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
419 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
420 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
421 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
422 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
425 03fa71c8 2018-09-06 stsp free(tree);
428 a440fac0 2018-09-06 stsp struct got_tree_entry *
429 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
431 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
433 32ac459c 2018-11-05 stsp te = malloc(sizeof(*te));
434 a440fac0 2018-09-06 stsp if (te == NULL)
435 a440fac0 2018-09-06 stsp return NULL;
437 32ac459c 2018-11-05 stsp te->id = malloc(sizeof(*te->id));
438 a440fac0 2018-09-06 stsp if (te->id == NULL) {
445 a440fac0 2018-09-06 stsp static const struct got_error *
446 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
447 a440fac0 2018-09-06 stsp size_t maxlen)
449 a440fac0 2018-09-06 stsp char *p = buf, *space;
450 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
452 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
453 a440fac0 2018-09-06 stsp if (*te == NULL)
454 a440fac0 2018-09-06 stsp return got_error_from_errno();
456 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
457 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
459 a440fac0 2018-09-06 stsp *te = NULL;
460 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
463 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
464 a440fac0 2018-09-06 stsp if (space == NULL) {
465 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
467 a440fac0 2018-09-06 stsp *te = NULL;
468 a440fac0 2018-09-06 stsp return err;
470 6dfaee02 2018-11-05 stsp (*te)->mode = 0;
471 a440fac0 2018-09-06 stsp while (*p != ' ') {
472 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
473 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
476 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
477 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
481 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
482 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
483 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
486 68bf1b1e 2018-11-07 stsp buf += *elen;
487 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
488 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
491 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
492 a440fac0 2018-09-06 stsp *te = NULL;
494 a440fac0 2018-09-06 stsp return err;
497 a440fac0 2018-09-06 stsp const struct got_error *
498 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
500 a440fac0 2018-09-06 stsp const struct got_error *err;
501 a440fac0 2018-09-06 stsp size_t remain = len;
503 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
504 a440fac0 2018-09-06 stsp if (*tree == NULL)
505 a440fac0 2018-09-06 stsp return got_error_from_errno();
507 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
509 a440fac0 2018-09-06 stsp while (remain > 0) {
510 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
511 a440fac0 2018-09-06 stsp size_t elen;
513 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
515 a440fac0 2018-09-06 stsp return err;
516 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
517 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
518 a440fac0 2018-09-06 stsp buf += elen;
519 a440fac0 2018-09-06 stsp remain -= elen;
522 a440fac0 2018-09-06 stsp if (remain != 0) {
523 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
524 13f977b4 2018-11-17 stsp *tree = NULL;
525 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
528 a440fac0 2018-09-06 stsp return NULL;
532 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
534 f4a881ce 2018-11-17 stsp free(tag->tag);
535 f4a881ce 2018-11-17 stsp free(tag->tagger);
536 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
540 ad242220 2018-09-08 stsp const struct got_error *
541 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
543 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
544 f4a881ce 2018-11-17 stsp size_t remain = len;
545 f4a881ce 2018-11-17 stsp char *s = buf;
546 f4a881ce 2018-11-17 stsp size_t tlen;
548 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
549 f4a881ce 2018-11-17 stsp if (*tag == NULL)
550 f4a881ce 2018-11-17 stsp return got_error_from_errno();
552 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_OBJECT);
553 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
554 f4a881ce 2018-11-17 stsp remain -= tlen;
555 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
556 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
560 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
561 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
564 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
565 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
567 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
571 f4a881ce 2018-11-17 stsp if (remain <= 0) {
572 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
576 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_TYPE);
577 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
578 f4a881ce 2018-11-17 stsp remain -= tlen;
579 f4a881ce 2018-11-17 stsp if (remain <= 0) {
580 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_OBJ_TAG_COMMIT,
585 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
586 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
587 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_COMMIT);
589 f4a881ce 2018-11-17 stsp remain -= tlen;
590 f4a881ce 2018-11-17 stsp } else if (strncmp(s, GOT_OBJ_TAG_TREE,
591 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_TREE)) == 0) {
592 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
593 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_TREE);
595 f4a881ce 2018-11-17 stsp remain -= tlen;
596 f4a881ce 2018-11-17 stsp } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
597 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_BLOB)) == 0) {
598 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
599 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_BLOB);
601 f4a881ce 2018-11-17 stsp remain -= tlen;
602 f4a881ce 2018-11-17 stsp } else if (strncmp(s, GOT_OBJ_TAG_TAG,
603 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_TAG)) == 0) {
604 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
605 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_TAG);
607 f4a881ce 2018-11-17 stsp remain -= tlen;
609 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
613 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
614 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
619 f4a881ce 2018-11-17 stsp if (remain <= 0) {
620 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
628 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_TAG);
629 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
631 f4a881ce 2018-11-17 stsp size_t slen;
632 f4a881ce 2018-11-17 stsp remain -= tlen;
633 f4a881ce 2018-11-17 stsp if (remain <= 0) {
634 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
638 f4a881ce 2018-11-17 stsp p = strchr(s, '\n');
639 f4a881ce 2018-11-17 stsp if (p == NULL) {
640 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 f4a881ce 2018-11-17 stsp slen = strlen(s);
645 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
646 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
647 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
650 f4a881ce 2018-11-17 stsp s += slen + 1;
651 f4a881ce 2018-11-17 stsp remain -= slen + 1;
652 f4a881ce 2018-11-17 stsp if (remain <= 0) {
653 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
657 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
661 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_TAGGER);
662 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
664 f4a881ce 2018-11-17 stsp size_t slen;
666 f4a881ce 2018-11-17 stsp remain -= tlen;
667 f4a881ce 2018-11-17 stsp if (remain <= 0) {
668 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
672 f4a881ce 2018-11-17 stsp p = strchr(s, '\n');
673 f4a881ce 2018-11-17 stsp if (p == NULL) {
674 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
678 f4a881ce 2018-11-17 stsp slen = strlen(s);
679 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
680 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
683 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
684 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
685 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
688 f4a881ce 2018-11-17 stsp s += slen + 1;
689 f4a881ce 2018-11-17 stsp remain -= slen + 1;
690 f4a881ce 2018-11-17 stsp if (remain <= 0) {
691 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
695 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
699 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
700 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
701 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
706 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
707 f4a881ce 2018-11-17 stsp *tag = NULL;
709 f4a881ce 2018-11-17 stsp return err;
712 f4a881ce 2018-11-17 stsp const struct got_error *
713 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
715 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
716 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
717 a440fac0 2018-09-06 stsp size_t n, total, remain;
718 a440fac0 2018-09-06 stsp uint8_t *buf;
720 a440fac0 2018-09-06 stsp *outbuf = NULL;
721 a440fac0 2018-09-06 stsp *outlen = 0;
723 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
724 a440fac0 2018-09-06 stsp if (buf == NULL)
725 a440fac0 2018-09-06 stsp return got_error_from_errno();
727 a440fac0 2018-09-06 stsp remain = blocksize;
729 a440fac0 2018-09-06 stsp while (1) {
730 a440fac0 2018-09-06 stsp if (remain == 0) {
731 a440fac0 2018-09-06 stsp uint8_t *newbuf;
732 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
733 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
734 a440fac0 2018-09-06 stsp err = got_error_from_errno();
737 a440fac0 2018-09-06 stsp buf = newbuf;
738 a440fac0 2018-09-06 stsp remain += blocksize;
740 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
741 a440fac0 2018-09-06 stsp if (n == 0) {
742 a440fac0 2018-09-06 stsp if (ferror(f)) {
743 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
746 a440fac0 2018-09-06 stsp break; /* EOF */
748 a440fac0 2018-09-06 stsp remain -= n;
749 a440fac0 2018-09-06 stsp total += n;
753 a440fac0 2018-09-06 stsp if (err == NULL) {
754 a440fac0 2018-09-06 stsp *outbuf = buf;
755 a440fac0 2018-09-06 stsp *outlen = total;
758 ad242220 2018-09-08 stsp return err;