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 a440fac0 2018-09-06 stsp #include "got_lib_inflate.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_object.h"
47 7762fe12 2018-11-05 stsp #include "got_lib_privsep.h"
48 7762fe12 2018-11-05 stsp #include "got_lib_pack.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 "
65 2ff12563 2018-09-15 stsp const struct got_error *
66 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
68 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
71 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
72 2ff12563 2018-09-15 stsp return got_error_from_errno();
74 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 2ff12563 2018-09-15 stsp free(*outbuf);
76 2ff12563 2018-09-15 stsp *outbuf = NULL;
77 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
80 2ff12563 2018-09-15 stsp return NULL;
84 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
86 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
87 03fa71c8 2018-09-06 stsp obj->refcnt--;
88 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
92 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
93 03fa71c8 2018-09-06 stsp struct got_delta *delta;
94 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
95 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
96 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
97 03fa71c8 2018-09-06 stsp got_delta_close(delta);
100 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
101 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
105 7762fe12 2018-11-05 stsp const struct got_error *
106 7762fe12 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
108 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
110 7762fe12 2018-11-05 stsp *qid = malloc(sizeof(**qid));
111 7762fe12 2018-11-05 stsp if (*qid == NULL)
112 7762fe12 2018-11-05 stsp return got_error_from_errno();
114 7762fe12 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
115 7762fe12 2018-11-05 stsp if ((*qid)->id == NULL) {
116 7762fe12 2018-11-05 stsp err = got_error_from_errno();
117 7762fe12 2018-11-05 stsp got_object_qid_free(*qid);
118 7762fe12 2018-11-05 stsp *qid = NULL;
120 7762fe12 2018-11-05 stsp return err;
124 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
126 03fa71c8 2018-09-06 stsp free(qid->id);
130 a440fac0 2018-09-06 stsp struct got_commit_object *
131 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
133 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
135 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
136 a440fac0 2018-09-06 stsp if (commit == NULL)
137 a440fac0 2018-09-06 stsp return NULL;
138 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
139 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
140 a440fac0 2018-09-06 stsp free(commit);
141 a440fac0 2018-09-06 stsp return NULL;
144 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
146 a440fac0 2018-09-06 stsp return commit;
149 05e1230b 2018-11-05 stsp struct got_mini_commit_object *
150 7762fe12 2018-11-05 stsp got_object_mini_commit_alloc_partial(void)
152 05e1230b 2018-11-05 stsp struct got_mini_commit_object *commit;
154 7762fe12 2018-11-05 stsp commit = calloc(1, sizeof(*commit));
155 7762fe12 2018-11-05 stsp if (commit == NULL)
156 7762fe12 2018-11-05 stsp return NULL;
157 7762fe12 2018-11-05 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
158 7762fe12 2018-11-05 stsp if (commit->tree_id == NULL) {
159 7762fe12 2018-11-05 stsp free(commit);
160 7762fe12 2018-11-05 stsp return NULL;
163 7762fe12 2018-11-05 stsp SIMPLEQ_INIT(&commit->parent_ids);
165 7762fe12 2018-11-05 stsp return commit;
168 a440fac0 2018-09-06 stsp const struct got_error *
169 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
170 a440fac0 2018-09-06 stsp const char *id_str)
172 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
173 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
175 7762fe12 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
177 a440fac0 2018-09-06 stsp return err;
179 7762fe12 2018-11-05 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
180 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 7762fe12 2018-11-05 stsp free(qid->id);
183 7762fe12 2018-11-05 stsp return err;
186 7762fe12 2018-11-05 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
187 7762fe12 2018-11-05 stsp commit->nparents++;
189 7762fe12 2018-11-05 stsp return NULL;
192 7762fe12 2018-11-05 stsp const struct got_error *
193 05e1230b 2018-11-05 stsp got_object_mini_commit_add_parent(struct got_mini_commit_object *commit,
194 7762fe12 2018-11-05 stsp const char *id_str)
196 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
197 7762fe12 2018-11-05 stsp struct got_object_qid *qid;
199 7762fe12 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
201 7762fe12 2018-11-05 stsp return err;
203 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
204 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
205 a440fac0 2018-09-06 stsp free(qid->id);
207 a440fac0 2018-09-06 stsp return err;
210 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
211 a440fac0 2018-09-06 stsp commit->nparents++;
213 a440fac0 2018-09-06 stsp return NULL;
216 a440fac0 2018-09-06 stsp static const struct got_error *
217 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
219 a440fac0 2018-09-06 stsp int sign = 1;
220 a440fac0 2018-09-06 stsp const char *p = tzstr;
221 a440fac0 2018-09-06 stsp time_t h, m;
223 a440fac0 2018-09-06 stsp *gmtoff = 0;
225 a440fac0 2018-09-06 stsp if (*p == '-')
227 a440fac0 2018-09-06 stsp else if (*p != '+')
228 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
230 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
231 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
232 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
235 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
236 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
237 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
239 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
240 a440fac0 2018-09-06 stsp return NULL;
243 a440fac0 2018-09-06 stsp static const struct got_error *
244 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
246 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
247 a440fac0 2018-09-06 stsp const char *errstr;
248 a440fac0 2018-09-06 stsp char *space, *tzstr;
249 a440fac0 2018-09-06 stsp time_t gmtoff;
250 a440fac0 2018-09-06 stsp time_t time;
252 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
253 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
254 a440fac0 2018-09-06 stsp if (space == NULL)
255 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
256 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
257 a440fac0 2018-09-06 stsp if (tzstr == NULL)
258 a440fac0 2018-09-06 stsp return got_error_from_errno();
259 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
260 a440fac0 2018-09-06 stsp free(tzstr);
262 a440fac0 2018-09-06 stsp return err;
263 a440fac0 2018-09-06 stsp *space = '\0';
265 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
266 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
267 a440fac0 2018-09-06 stsp if (space == NULL)
268 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
270 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
271 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
272 a440fac0 2018-09-06 stsp if (errstr)
273 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
275 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
276 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
277 a440fac0 2018-09-06 stsp time -= gmtoff;
278 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
279 a440fac0 2018-09-06 stsp return got_error_from_errno();
280 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
282 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
283 a440fac0 2018-09-06 stsp *space = '\0';
285 a440fac0 2018-09-06 stsp return NULL;
289 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
291 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
293 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
294 03fa71c8 2018-09-06 stsp commit->refcnt--;
295 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
299 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
300 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
301 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
302 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
305 03fa71c8 2018-09-06 stsp free(commit->tree_id);
306 03fa71c8 2018-09-06 stsp free(commit->author);
307 03fa71c8 2018-09-06 stsp free(commit->committer);
308 03fa71c8 2018-09-06 stsp free(commit->logmsg);
309 03fa71c8 2018-09-06 stsp free(commit);
313 05e1230b 2018-11-05 stsp got_object_mini_commit_close(struct got_mini_commit_object *commit)
315 7762fe12 2018-11-05 stsp struct got_object_qid *qid;
317 e32baab7 2018-11-05 stsp if (commit->refcnt > 0) {
318 e32baab7 2018-11-05 stsp commit->refcnt--;
319 e32baab7 2018-11-05 stsp if (commit->refcnt > 0)
323 7762fe12 2018-11-05 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
324 7762fe12 2018-11-05 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
325 7762fe12 2018-11-05 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
326 7762fe12 2018-11-05 stsp got_object_qid_free(qid);
329 7762fe12 2018-11-05 stsp free(commit->tree_id);
330 7762fe12 2018-11-05 stsp free(commit);
333 a440fac0 2018-09-06 stsp const struct got_error *
334 7762fe12 2018-11-05 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
335 7762fe12 2018-11-05 stsp size_t len)
337 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
338 a440fac0 2018-09-06 stsp char *s = buf;
339 a440fac0 2018-09-06 stsp size_t tlen;
340 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
342 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
343 a440fac0 2018-09-06 stsp if (*commit == NULL)
344 a440fac0 2018-09-06 stsp return got_error_from_errno();
346 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
347 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
348 a440fac0 2018-09-06 stsp remain -= tlen;
349 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
350 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
354 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
355 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
358 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
359 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
361 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
365 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
366 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
367 a440fac0 2018-09-06 stsp remain -= tlen;
368 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
369 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
377 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
378 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
381 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
382 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
384 a440fac0 2018-09-06 stsp size_t slen;
386 a440fac0 2018-09-06 stsp remain -= tlen;
387 a440fac0 2018-09-06 stsp if (remain <= 0) {
388 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
393 a440fac0 2018-09-06 stsp if (p == NULL) {
394 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
398 a440fac0 2018-09-06 stsp slen = strlen(s);
399 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
402 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
403 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
404 a440fac0 2018-09-06 stsp err = got_error_from_errno();
407 a440fac0 2018-09-06 stsp s += slen + 1;
408 a440fac0 2018-09-06 stsp remain -= slen + 1;
411 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
412 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
414 a440fac0 2018-09-06 stsp size_t slen;
416 a440fac0 2018-09-06 stsp remain -= tlen;
417 a440fac0 2018-09-06 stsp if (remain <= 0) {
418 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
422 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
423 a440fac0 2018-09-06 stsp if (p == NULL) {
424 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
428 a440fac0 2018-09-06 stsp slen = strlen(s);
429 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
432 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
433 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
434 a440fac0 2018-09-06 stsp err = got_error_from_errno();
437 a440fac0 2018-09-06 stsp s += slen + 1;
438 a440fac0 2018-09-06 stsp remain -= slen + 1;
441 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
442 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
443 a440fac0 2018-09-06 stsp err = got_error_from_errno();
448 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
449 a440fac0 2018-09-06 stsp *commit = NULL;
451 a440fac0 2018-09-06 stsp return err;
454 7762fe12 2018-11-05 stsp const struct got_error *
455 05e1230b 2018-11-05 stsp got_object_parse_mini_commit(struct got_mini_commit_object **commit, char *buf,
456 7762fe12 2018-11-05 stsp size_t len)
458 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
459 7762fe12 2018-11-05 stsp char *s = buf;
460 7762fe12 2018-11-05 stsp size_t tlen;
461 7762fe12 2018-11-05 stsp ssize_t remain = (ssize_t)len;
463 7762fe12 2018-11-05 stsp *commit = got_object_mini_commit_alloc_partial();
464 7762fe12 2018-11-05 stsp if (*commit == NULL)
465 7762fe12 2018-11-05 stsp return got_error_from_errno();
467 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
468 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
469 7762fe12 2018-11-05 stsp remain -= tlen;
470 7762fe12 2018-11-05 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
471 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
475 7762fe12 2018-11-05 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
476 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
479 7762fe12 2018-11-05 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
480 7762fe12 2018-11-05 stsp s += SHA1_DIGEST_STRING_LENGTH;
482 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
486 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
487 7762fe12 2018-11-05 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
488 7762fe12 2018-11-05 stsp remain -= tlen;
489 7762fe12 2018-11-05 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
490 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
494 7762fe12 2018-11-05 stsp err = got_object_mini_commit_add_parent(*commit, s);
498 7762fe12 2018-11-05 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
499 7762fe12 2018-11-05 stsp s += SHA1_DIGEST_STRING_LENGTH;
502 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
503 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
505 7762fe12 2018-11-05 stsp size_t slen;
507 7762fe12 2018-11-05 stsp remain -= tlen;
508 7762fe12 2018-11-05 stsp if (remain <= 0) {
509 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
513 7762fe12 2018-11-05 stsp p = strchr(s, '\n');
514 7762fe12 2018-11-05 stsp if (p == NULL) {
515 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
519 7762fe12 2018-11-05 stsp slen = strlen(s);
520 7762fe12 2018-11-05 stsp s += slen + 1;
521 7762fe12 2018-11-05 stsp remain -= slen + 1;
524 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
527 7762fe12 2018-11-05 stsp size_t slen;
529 7762fe12 2018-11-05 stsp remain -= tlen;
530 7762fe12 2018-11-05 stsp if (remain <= 0) {
531 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
535 7762fe12 2018-11-05 stsp p = strchr(s, '\n');
536 7762fe12 2018-11-05 stsp if (p == NULL) {
537 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
541 7762fe12 2018-11-05 stsp slen = strlen(s);
542 7762fe12 2018-11-05 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
545 7762fe12 2018-11-05 stsp s += slen + 1;
546 7762fe12 2018-11-05 stsp remain -= slen + 1;
551 7762fe12 2018-11-05 stsp got_object_mini_commit_close(*commit);
552 7762fe12 2018-11-05 stsp *commit = NULL;
554 7762fe12 2018-11-05 stsp return err;
558 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
560 a440fac0 2018-09-06 stsp free(te->id);
561 a440fac0 2018-09-06 stsp free(te->name);
566 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
568 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
570 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
571 03fa71c8 2018-09-06 stsp tree->refcnt--;
572 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
576 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
577 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
578 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
579 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
582 03fa71c8 2018-09-06 stsp free(tree);
585 a440fac0 2018-09-06 stsp struct got_tree_entry *
586 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
588 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
590 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
591 a440fac0 2018-09-06 stsp if (te == NULL)
592 a440fac0 2018-09-06 stsp return NULL;
594 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
595 a440fac0 2018-09-06 stsp if (te->id == NULL) {
602 a440fac0 2018-09-06 stsp static const struct got_error *
603 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
604 a440fac0 2018-09-06 stsp size_t maxlen)
606 a440fac0 2018-09-06 stsp char *p = buf, *space;
607 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
609 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
610 a440fac0 2018-09-06 stsp if (*te == NULL)
611 a440fac0 2018-09-06 stsp return got_error_from_errno();
613 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
614 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
616 a440fac0 2018-09-06 stsp *te = NULL;
617 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
620 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
621 a440fac0 2018-09-06 stsp if (space == NULL) {
622 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 a440fac0 2018-09-06 stsp *te = NULL;
625 a440fac0 2018-09-06 stsp return err;
627 a440fac0 2018-09-06 stsp while (*p != ' ') {
628 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
629 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
633 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
637 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
638 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
639 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
643 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
644 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
647 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
648 a440fac0 2018-09-06 stsp *te = NULL;
650 a440fac0 2018-09-06 stsp return err;
653 a440fac0 2018-09-06 stsp const struct got_error *
654 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
656 a440fac0 2018-09-06 stsp const struct got_error *err;
657 a440fac0 2018-09-06 stsp size_t remain = len;
659 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
660 a440fac0 2018-09-06 stsp if (*tree == NULL)
661 a440fac0 2018-09-06 stsp return got_error_from_errno();
663 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
665 a440fac0 2018-09-06 stsp while (remain > 0) {
666 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
667 a440fac0 2018-09-06 stsp size_t elen;
669 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
671 a440fac0 2018-09-06 stsp return err;
672 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
673 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
674 a440fac0 2018-09-06 stsp buf += elen;
675 a440fac0 2018-09-06 stsp remain -= elen;
678 a440fac0 2018-09-06 stsp if (remain != 0) {
679 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
680 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
683 a440fac0 2018-09-06 stsp return NULL;
686 ad242220 2018-09-08 stsp const struct got_error *
687 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
689 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
690 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
691 a440fac0 2018-09-06 stsp size_t n, total, remain;
692 a440fac0 2018-09-06 stsp uint8_t *buf;
694 a440fac0 2018-09-06 stsp *outbuf = NULL;
695 a440fac0 2018-09-06 stsp *outlen = 0;
697 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
698 a440fac0 2018-09-06 stsp if (buf == NULL)
699 a440fac0 2018-09-06 stsp return got_error_from_errno();
701 a440fac0 2018-09-06 stsp remain = blocksize;
703 a440fac0 2018-09-06 stsp while (1) {
704 a440fac0 2018-09-06 stsp if (remain == 0) {
705 a440fac0 2018-09-06 stsp uint8_t *newbuf;
706 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
707 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
708 a440fac0 2018-09-06 stsp err = got_error_from_errno();
711 a440fac0 2018-09-06 stsp buf = newbuf;
712 a440fac0 2018-09-06 stsp remain += blocksize;
714 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
715 a440fac0 2018-09-06 stsp if (n == 0) {
716 a440fac0 2018-09-06 stsp if (ferror(f)) {
717 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
720 a440fac0 2018-09-06 stsp break; /* EOF */
722 a440fac0 2018-09-06 stsp remain -= n;
723 a440fac0 2018-09-06 stsp total += n;
727 a440fac0 2018-09-06 stsp if (err == NULL) {
728 a440fac0 2018-09-06 stsp *outbuf = buf;
729 a440fac0 2018-09-06 stsp *outlen = total;
732 ad242220 2018-09-08 stsp return err;