Blame


1 a440fac0 2018-09-06 stsp /*
2 a440fac0 2018-09-06 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 a440fac0 2018-09-06 stsp *
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.
7 a440fac0 2018-09-06 stsp *
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.
15 a440fac0 2018-09-06 stsp */
16 a440fac0 2018-09-06 stsp
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>
24 a440fac0 2018-09-06 stsp
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>
37 a440fac0 2018-09-06 stsp
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"
42 a440fac0 2018-09-06 stsp
43 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
44 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
45 41fa1437 2018-11-05 stsp #include "got_lib_inflate.h"
46 41fa1437 2018-11-05 stsp #include "got_lib_object.h"
47 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
48 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
49 15a94983 2018-12-23 stsp #include "got_lib_privsep.h"
50 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
51 a440fac0 2018-09-06 stsp
52 a440fac0 2018-09-06 stsp #ifndef nitems
53 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
54 a440fac0 2018-09-06 stsp #endif
55 a440fac0 2018-09-06 stsp
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"
60 f4a881ce 2018-11-17 stsp
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 "
65 2ff12563 2018-09-15 stsp
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 "
70 f4a881ce 2018-11-17 stsp
71 f054b67a 2018-11-05 stsp int
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)
74 f054b67a 2018-11-05 stsp {
75 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
76 f054b67a 2018-11-05 stsp }
77 f054b67a 2018-11-05 stsp
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)
80 5df4932d 2018-11-05 stsp {
81 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
82 5df4932d 2018-11-05 stsp
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();
86 5df4932d 2018-11-05 stsp
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;
92 5df4932d 2018-11-05 stsp return err;
93 5df4932d 2018-11-05 stsp }
94 5df4932d 2018-11-05 stsp
95 5df4932d 2018-11-05 stsp return NULL;
96 5df4932d 2018-11-05 stsp }
97 5df4932d 2018-11-05 stsp
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)
100 2ff12563 2018-09-15 stsp {
101 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
102 2ff12563 2018-09-15 stsp
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();
106 2ff12563 2018-09-15 stsp
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);
111 2ff12563 2018-09-15 stsp }
112 a440fac0 2018-09-06 stsp
113 2ff12563 2018-09-15 stsp return NULL;
114 2ff12563 2018-09-15 stsp }
115 2ff12563 2018-09-15 stsp
116 03fa71c8 2018-09-06 stsp void
117 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
118 03fa71c8 2018-09-06 stsp {
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)
122 03fa71c8 2018-09-06 stsp return;
123 03fa71c8 2018-09-06 stsp }
124 03fa71c8 2018-09-06 stsp
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);
131 03fa71c8 2018-09-06 stsp }
132 03fa71c8 2018-09-06 stsp }
133 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
134 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
135 03fa71c8 2018-09-06 stsp free(obj);
136 03fa71c8 2018-09-06 stsp }
137 03fa71c8 2018-09-06 stsp
138 03fa71c8 2018-09-06 stsp void
139 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
140 03fa71c8 2018-09-06 stsp {
141 03fa71c8 2018-09-06 stsp free(qid->id);
142 03fa71c8 2018-09-06 stsp free(qid);
143 876c234b 2018-09-10 stsp }
144 876c234b 2018-09-10 stsp
145 a440fac0 2018-09-06 stsp struct got_commit_object *
146 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
147 a440fac0 2018-09-06 stsp {
148 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
149 a440fac0 2018-09-06 stsp
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;
157 a440fac0 2018-09-06 stsp }
158 a440fac0 2018-09-06 stsp
159 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
160 a440fac0 2018-09-06 stsp
161 a440fac0 2018-09-06 stsp return commit;
162 a440fac0 2018-09-06 stsp }
163 a440fac0 2018-09-06 stsp
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)
167 a440fac0 2018-09-06 stsp {
168 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
169 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
170 a440fac0 2018-09-06 stsp
171 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
172 5df4932d 2018-11-05 stsp if (err)
173 7762fe12 2018-11-05 stsp return err;
174 a440fac0 2018-09-06 stsp
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);
178 a440fac0 2018-09-06 stsp free(qid);
179 a440fac0 2018-09-06 stsp return err;
180 a440fac0 2018-09-06 stsp }
181 a440fac0 2018-09-06 stsp
182 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
183 a440fac0 2018-09-06 stsp commit->nparents++;
184 a440fac0 2018-09-06 stsp
185 a440fac0 2018-09-06 stsp return NULL;
186 a440fac0 2018-09-06 stsp }
187 a440fac0 2018-09-06 stsp
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)
190 a440fac0 2018-09-06 stsp {
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;
194 a440fac0 2018-09-06 stsp
195 a440fac0 2018-09-06 stsp *gmtoff = 0;
196 a440fac0 2018-09-06 stsp
197 a440fac0 2018-09-06 stsp if (*p == '-')
198 a440fac0 2018-09-06 stsp sign = -1;
199 a440fac0 2018-09-06 stsp else if (*p != '+')
200 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
201 a440fac0 2018-09-06 stsp p++;
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'));
205 a440fac0 2018-09-06 stsp
206 a440fac0 2018-09-06 stsp p += 2;
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');
210 a440fac0 2018-09-06 stsp
211 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
212 a440fac0 2018-09-06 stsp return NULL;
213 a440fac0 2018-09-06 stsp }
214 a440fac0 2018-09-06 stsp
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)
217 a440fac0 2018-09-06 stsp {
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;
221 a440fac0 2018-09-06 stsp
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);
231 a440fac0 2018-09-06 stsp if (err)
232 a440fac0 2018-09-06 stsp return err;
233 a440fac0 2018-09-06 stsp *space = '\0';
234 a440fac0 2018-09-06 stsp
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);
239 a440fac0 2018-09-06 stsp
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);
244 a440fac0 2018-09-06 stsp
245 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
246 ccb26ccd 2018-11-05 stsp *time -= *gmtoff;
247 a440fac0 2018-09-06 stsp
248 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
249 a440fac0 2018-09-06 stsp *space = '\0';
250 a440fac0 2018-09-06 stsp
251 a440fac0 2018-09-06 stsp return NULL;
252 a440fac0 2018-09-06 stsp }
253 a440fac0 2018-09-06 stsp
254 03fa71c8 2018-09-06 stsp void
255 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
256 03fa71c8 2018-09-06 stsp {
257 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
258 03fa71c8 2018-09-06 stsp
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)
262 03fa71c8 2018-09-06 stsp return;
263 03fa71c8 2018-09-06 stsp }
264 03fa71c8 2018-09-06 stsp
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);
269 03fa71c8 2018-09-06 stsp }
270 03fa71c8 2018-09-06 stsp
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);
276 45d799e2 2018-12-23 stsp }
277 45d799e2 2018-12-23 stsp
278 45d799e2 2018-12-23 stsp struct got_object_id *
279 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
280 45d799e2 2018-12-23 stsp {
281 45d799e2 2018-12-23 stsp return commit->tree_id;
282 45d799e2 2018-12-23 stsp }
283 45d799e2 2018-12-23 stsp
284 45d799e2 2018-12-23 stsp int
285 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
286 45d799e2 2018-12-23 stsp {
287 45d799e2 2018-12-23 stsp return commit->nparents;
288 03fa71c8 2018-09-06 stsp }
289 03fa71c8 2018-09-06 stsp
290 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
291 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
292 45d799e2 2018-12-23 stsp {
293 45d799e2 2018-12-23 stsp return &commit->parent_ids;
294 45d799e2 2018-12-23 stsp }
295 45d799e2 2018-12-23 stsp
296 45d799e2 2018-12-23 stsp const char *
297 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
298 45d799e2 2018-12-23 stsp {
299 45d799e2 2018-12-23 stsp return commit->author;
300 45d799e2 2018-12-23 stsp }
301 45d799e2 2018-12-23 stsp
302 45d799e2 2018-12-23 stsp time_t
303 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
304 45d799e2 2018-12-23 stsp {
305 45d799e2 2018-12-23 stsp return commit->author_time;
306 45d799e2 2018-12-23 stsp }
307 45d799e2 2018-12-23 stsp
308 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
309 45d799e2 2018-12-23 stsp {
310 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
311 45d799e2 2018-12-23 stsp }
312 45d799e2 2018-12-23 stsp
313 45d799e2 2018-12-23 stsp const char *
314 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
315 45d799e2 2018-12-23 stsp {
316 45d799e2 2018-12-23 stsp return commit->committer;
317 45d799e2 2018-12-23 stsp }
318 45d799e2 2018-12-23 stsp
319 45d799e2 2018-12-23 stsp time_t
320 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
321 45d799e2 2018-12-23 stsp {
322 45d799e2 2018-12-23 stsp return commit->committer_time;
323 45d799e2 2018-12-23 stsp }
324 45d799e2 2018-12-23 stsp
325 45d799e2 2018-12-23 stsp time_t
326 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
327 45d799e2 2018-12-23 stsp {
328 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
329 45d799e2 2018-12-23 stsp }
330 45d799e2 2018-12-23 stsp
331 45d799e2 2018-12-23 stsp const char *
332 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(struct got_commit_object *commit)
333 45d799e2 2018-12-23 stsp {
334 45d799e2 2018-12-23 stsp return commit->logmsg;
335 45d799e2 2018-12-23 stsp }
336 45d799e2 2018-12-23 stsp
337 a440fac0 2018-09-06 stsp const struct got_error *
338 41fa1437 2018-11-05 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
339 a440fac0 2018-09-06 stsp {
340 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
341 a440fac0 2018-09-06 stsp char *s = buf;
342 a440fac0 2018-09-06 stsp size_t tlen;
343 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
344 41fa1437 2018-11-05 stsp
345 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
346 a440fac0 2018-09-06 stsp if (*commit == NULL)
347 a440fac0 2018-09-06 stsp return got_error_from_errno();
348 a440fac0 2018-09-06 stsp
349 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
350 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
351 a440fac0 2018-09-06 stsp remain -= tlen;
352 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
353 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
354 a440fac0 2018-09-06 stsp goto done;
355 a440fac0 2018-09-06 stsp }
356 a440fac0 2018-09-06 stsp s += tlen;
357 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
358 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
359 a440fac0 2018-09-06 stsp goto done;
360 a440fac0 2018-09-06 stsp }
361 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
362 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
363 a440fac0 2018-09-06 stsp } else {
364 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
365 a440fac0 2018-09-06 stsp goto done;
366 a440fac0 2018-09-06 stsp }
367 a440fac0 2018-09-06 stsp
368 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
369 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
370 a440fac0 2018-09-06 stsp remain -= tlen;
371 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
372 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
373 a440fac0 2018-09-06 stsp goto done;
374 a440fac0 2018-09-06 stsp }
375 a440fac0 2018-09-06 stsp s += tlen;
376 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
377 a440fac0 2018-09-06 stsp if (err)
378 a440fac0 2018-09-06 stsp goto done;
379 a440fac0 2018-09-06 stsp
380 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
381 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
382 a440fac0 2018-09-06 stsp }
383 a440fac0 2018-09-06 stsp
384 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
385 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
386 a440fac0 2018-09-06 stsp char *p;
387 a440fac0 2018-09-06 stsp size_t slen;
388 a440fac0 2018-09-06 stsp
389 a440fac0 2018-09-06 stsp remain -= tlen;
390 a440fac0 2018-09-06 stsp if (remain <= 0) {
391 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
392 a440fac0 2018-09-06 stsp goto done;
393 a440fac0 2018-09-06 stsp }
394 a440fac0 2018-09-06 stsp s += tlen;
395 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
396 a440fac0 2018-09-06 stsp if (p == NULL) {
397 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
398 a440fac0 2018-09-06 stsp goto done;
399 a440fac0 2018-09-06 stsp }
400 a440fac0 2018-09-06 stsp *p = '\0';
401 a440fac0 2018-09-06 stsp slen = strlen(s);
402 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
403 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
404 a440fac0 2018-09-06 stsp if (err)
405 a440fac0 2018-09-06 stsp goto done;
406 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
407 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
408 a440fac0 2018-09-06 stsp err = got_error_from_errno();
409 a440fac0 2018-09-06 stsp goto done;
410 a440fac0 2018-09-06 stsp }
411 a440fac0 2018-09-06 stsp s += slen + 1;
412 a440fac0 2018-09-06 stsp remain -= slen + 1;
413 a440fac0 2018-09-06 stsp }
414 a440fac0 2018-09-06 stsp
415 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
416 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
417 a440fac0 2018-09-06 stsp char *p;
418 a440fac0 2018-09-06 stsp size_t slen;
419 a440fac0 2018-09-06 stsp
420 a440fac0 2018-09-06 stsp remain -= tlen;
421 a440fac0 2018-09-06 stsp if (remain <= 0) {
422 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
423 a440fac0 2018-09-06 stsp goto done;
424 a440fac0 2018-09-06 stsp }
425 a440fac0 2018-09-06 stsp s += tlen;
426 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
427 a440fac0 2018-09-06 stsp if (p == NULL) {
428 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
429 a440fac0 2018-09-06 stsp goto done;
430 a440fac0 2018-09-06 stsp }
431 a440fac0 2018-09-06 stsp *p = '\0';
432 a440fac0 2018-09-06 stsp slen = strlen(s);
433 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
434 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
435 a440fac0 2018-09-06 stsp if (err)
436 a440fac0 2018-09-06 stsp goto done;
437 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
438 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
439 a440fac0 2018-09-06 stsp err = got_error_from_errno();
440 a440fac0 2018-09-06 stsp goto done;
441 a440fac0 2018-09-06 stsp }
442 a440fac0 2018-09-06 stsp s += slen + 1;
443 a440fac0 2018-09-06 stsp remain -= slen + 1;
444 a440fac0 2018-09-06 stsp }
445 a440fac0 2018-09-06 stsp
446 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
447 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
448 a440fac0 2018-09-06 stsp err = got_error_from_errno();
449 a440fac0 2018-09-06 stsp goto done;
450 a440fac0 2018-09-06 stsp }
451 a440fac0 2018-09-06 stsp done:
452 a440fac0 2018-09-06 stsp if (err) {
453 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
454 a440fac0 2018-09-06 stsp *commit = NULL;
455 a440fac0 2018-09-06 stsp }
456 a440fac0 2018-09-06 stsp return err;
457 a440fac0 2018-09-06 stsp }
458 a440fac0 2018-09-06 stsp
459 ad242220 2018-09-08 stsp void
460 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
461 a440fac0 2018-09-06 stsp {
462 a440fac0 2018-09-06 stsp free(te->id);
463 a440fac0 2018-09-06 stsp free(te->name);
464 a440fac0 2018-09-06 stsp free(te);
465 a440fac0 2018-09-06 stsp }
466 a440fac0 2018-09-06 stsp
467 03fa71c8 2018-09-06 stsp void
468 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
469 03fa71c8 2018-09-06 stsp {
470 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
471 03fa71c8 2018-09-06 stsp
472 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
473 03fa71c8 2018-09-06 stsp tree->refcnt--;
474 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
475 03fa71c8 2018-09-06 stsp return;
476 03fa71c8 2018-09-06 stsp }
477 03fa71c8 2018-09-06 stsp
478 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
479 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
480 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
481 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
482 03fa71c8 2018-09-06 stsp }
483 03fa71c8 2018-09-06 stsp
484 03fa71c8 2018-09-06 stsp free(tree);
485 03fa71c8 2018-09-06 stsp }
486 03fa71c8 2018-09-06 stsp
487 a440fac0 2018-09-06 stsp struct got_tree_entry *
488 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
489 a440fac0 2018-09-06 stsp {
490 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
491 a440fac0 2018-09-06 stsp
492 32ac459c 2018-11-05 stsp te = malloc(sizeof(*te));
493 a440fac0 2018-09-06 stsp if (te == NULL)
494 a440fac0 2018-09-06 stsp return NULL;
495 a440fac0 2018-09-06 stsp
496 32ac459c 2018-11-05 stsp te->id = malloc(sizeof(*te->id));
497 a440fac0 2018-09-06 stsp if (te->id == NULL) {
498 a440fac0 2018-09-06 stsp free(te);
499 a440fac0 2018-09-06 stsp te = NULL;
500 a440fac0 2018-09-06 stsp }
501 a440fac0 2018-09-06 stsp return te;
502 a440fac0 2018-09-06 stsp }
503 a440fac0 2018-09-06 stsp
504 a440fac0 2018-09-06 stsp static const struct got_error *
505 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
506 a440fac0 2018-09-06 stsp size_t maxlen)
507 a440fac0 2018-09-06 stsp {
508 a440fac0 2018-09-06 stsp char *p = buf, *space;
509 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
510 a440fac0 2018-09-06 stsp
511 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
512 a440fac0 2018-09-06 stsp if (*te == NULL)
513 a440fac0 2018-09-06 stsp return got_error_from_errno();
514 a440fac0 2018-09-06 stsp
515 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
516 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
517 a440fac0 2018-09-06 stsp free(*te);
518 a440fac0 2018-09-06 stsp *te = NULL;
519 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
520 a440fac0 2018-09-06 stsp }
521 a440fac0 2018-09-06 stsp
522 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
523 a440fac0 2018-09-06 stsp if (space == NULL) {
524 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
525 a440fac0 2018-09-06 stsp free(*te);
526 a440fac0 2018-09-06 stsp *te = NULL;
527 a440fac0 2018-09-06 stsp return err;
528 a440fac0 2018-09-06 stsp }
529 6dfaee02 2018-11-05 stsp (*te)->mode = 0;
530 a440fac0 2018-09-06 stsp while (*p != ' ') {
531 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
532 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
533 a440fac0 2018-09-06 stsp goto done;
534 a440fac0 2018-09-06 stsp }
535 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
536 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
537 a440fac0 2018-09-06 stsp p++;
538 a440fac0 2018-09-06 stsp }
539 a440fac0 2018-09-06 stsp
540 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
541 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
542 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
543 a440fac0 2018-09-06 stsp goto done;
544 a440fac0 2018-09-06 stsp }
545 68bf1b1e 2018-11-07 stsp buf += *elen;
546 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
547 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
548 a440fac0 2018-09-06 stsp done:
549 a440fac0 2018-09-06 stsp if (err) {
550 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
551 a440fac0 2018-09-06 stsp *te = NULL;
552 a440fac0 2018-09-06 stsp }
553 a440fac0 2018-09-06 stsp return err;
554 a440fac0 2018-09-06 stsp }
555 a440fac0 2018-09-06 stsp
556 a440fac0 2018-09-06 stsp const struct got_error *
557 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
558 a440fac0 2018-09-06 stsp {
559 a440fac0 2018-09-06 stsp const struct got_error *err;
560 a440fac0 2018-09-06 stsp size_t remain = len;
561 a440fac0 2018-09-06 stsp
562 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
563 a440fac0 2018-09-06 stsp if (*tree == NULL)
564 a440fac0 2018-09-06 stsp return got_error_from_errno();
565 a440fac0 2018-09-06 stsp
566 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
567 a440fac0 2018-09-06 stsp
568 a440fac0 2018-09-06 stsp while (remain > 0) {
569 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
570 a440fac0 2018-09-06 stsp size_t elen;
571 a440fac0 2018-09-06 stsp
572 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
573 a440fac0 2018-09-06 stsp if (err)
574 a440fac0 2018-09-06 stsp return err;
575 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
576 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
577 a440fac0 2018-09-06 stsp buf += elen;
578 a440fac0 2018-09-06 stsp remain -= elen;
579 a440fac0 2018-09-06 stsp }
580 a440fac0 2018-09-06 stsp
581 a440fac0 2018-09-06 stsp if (remain != 0) {
582 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
583 13f977b4 2018-11-17 stsp *tree = NULL;
584 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
585 a440fac0 2018-09-06 stsp }
586 a440fac0 2018-09-06 stsp
587 a440fac0 2018-09-06 stsp return NULL;
588 a440fac0 2018-09-06 stsp }
589 a440fac0 2018-09-06 stsp
590 f4a881ce 2018-11-17 stsp void
591 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
592 f4a881ce 2018-11-17 stsp {
593 f4a881ce 2018-11-17 stsp free(tag->tag);
594 f4a881ce 2018-11-17 stsp free(tag->tagger);
595 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
596 f4a881ce 2018-11-17 stsp free(tag);
597 f4a881ce 2018-11-17 stsp }
598 f4a881ce 2018-11-17 stsp
599 ad242220 2018-09-08 stsp const struct got_error *
600 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
601 f4a881ce 2018-11-17 stsp {
602 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
603 f4a881ce 2018-11-17 stsp size_t remain = len;
604 f4a881ce 2018-11-17 stsp char *s = buf;
605 f4a881ce 2018-11-17 stsp size_t tlen;
606 f4a881ce 2018-11-17 stsp
607 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
608 f4a881ce 2018-11-17 stsp if (*tag == NULL)
609 f4a881ce 2018-11-17 stsp return got_error_from_errno();
610 f4a881ce 2018-11-17 stsp
611 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_OBJECT);
612 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_OBJECT, tlen) == 0) {
613 f4a881ce 2018-11-17 stsp remain -= tlen;
614 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
615 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
616 f4a881ce 2018-11-17 stsp goto done;
617 f4a881ce 2018-11-17 stsp }
618 f4a881ce 2018-11-17 stsp s += tlen;
619 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
620 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
621 f4a881ce 2018-11-17 stsp goto done;
622 f4a881ce 2018-11-17 stsp }
623 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
624 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
625 f4a881ce 2018-11-17 stsp } else {
626 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
627 f4a881ce 2018-11-17 stsp goto done;
628 f4a881ce 2018-11-17 stsp }
629 f4a881ce 2018-11-17 stsp
630 f4a881ce 2018-11-17 stsp if (remain <= 0) {
631 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 f4a881ce 2018-11-17 stsp goto done;
633 f4a881ce 2018-11-17 stsp }
634 f4a881ce 2018-11-17 stsp
635 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_TYPE);
636 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_TYPE, tlen) == 0) {
637 f4a881ce 2018-11-17 stsp remain -= tlen;
638 f4a881ce 2018-11-17 stsp if (remain <= 0) {
639 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 f4a881ce 2018-11-17 stsp goto done;
641 f4a881ce 2018-11-17 stsp }
642 f4a881ce 2018-11-17 stsp s += tlen;
643 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_OBJ_TAG_COMMIT,
644 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_COMMIT)) == 0) {
645 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
646 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_COMMIT);
647 f4a881ce 2018-11-17 stsp s += tlen;
648 f4a881ce 2018-11-17 stsp remain -= tlen;
649 f4a881ce 2018-11-17 stsp } else if (strncmp(s, GOT_OBJ_TAG_TREE,
650 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_TREE)) == 0) {
651 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
652 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_TREE);
653 f4a881ce 2018-11-17 stsp s += tlen;
654 f4a881ce 2018-11-17 stsp remain -= tlen;
655 f4a881ce 2018-11-17 stsp } else if (strncmp(s, GOT_OBJ_TAG_BLOB,
656 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_BLOB)) == 0) {
657 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
658 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_BLOB);
659 f4a881ce 2018-11-17 stsp s += tlen;
660 f4a881ce 2018-11-17 stsp remain -= tlen;
661 f4a881ce 2018-11-17 stsp } else if (strncmp(s, GOT_OBJ_TAG_TAG,
662 f4a881ce 2018-11-17 stsp strlen(GOT_OBJ_TAG_TAG)) == 0) {
663 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
664 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_OBJ_TAG_TAG);
665 f4a881ce 2018-11-17 stsp s += tlen;
666 f4a881ce 2018-11-17 stsp remain -= tlen;
667 f4a881ce 2018-11-17 stsp } else {
668 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
669 f4a881ce 2018-11-17 stsp goto done;
670 f4a881ce 2018-11-17 stsp }
671 f4a881ce 2018-11-17 stsp
672 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
673 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 f4a881ce 2018-11-17 stsp goto done;
675 f4a881ce 2018-11-17 stsp }
676 f4a881ce 2018-11-17 stsp s++;
677 f4a881ce 2018-11-17 stsp remain--;
678 f4a881ce 2018-11-17 stsp if (remain <= 0) {
679 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
680 f4a881ce 2018-11-17 stsp goto done;
681 f4a881ce 2018-11-17 stsp }
682 f4a881ce 2018-11-17 stsp } else {
683 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
684 f4a881ce 2018-11-17 stsp goto done;
685 f4a881ce 2018-11-17 stsp }
686 f4a881ce 2018-11-17 stsp
687 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_TAG);
688 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_TAG, tlen) == 0) {
689 f4a881ce 2018-11-17 stsp char *p;
690 f4a881ce 2018-11-17 stsp size_t slen;
691 f4a881ce 2018-11-17 stsp remain -= tlen;
692 f4a881ce 2018-11-17 stsp if (remain <= 0) {
693 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 f4a881ce 2018-11-17 stsp goto done;
695 f4a881ce 2018-11-17 stsp }
696 f4a881ce 2018-11-17 stsp s += tlen;
697 f4a881ce 2018-11-17 stsp p = strchr(s, '\n');
698 f4a881ce 2018-11-17 stsp if (p == NULL) {
699 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
700 f4a881ce 2018-11-17 stsp goto done;
701 f4a881ce 2018-11-17 stsp }
702 f4a881ce 2018-11-17 stsp *p = '\0';
703 f4a881ce 2018-11-17 stsp slen = strlen(s);
704 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
705 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
706 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
707 f4a881ce 2018-11-17 stsp goto done;
708 f4a881ce 2018-11-17 stsp }
709 f4a881ce 2018-11-17 stsp s += slen + 1;
710 f4a881ce 2018-11-17 stsp remain -= slen + 1;
711 f4a881ce 2018-11-17 stsp if (remain <= 0) {
712 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 f4a881ce 2018-11-17 stsp goto done;
714 f4a881ce 2018-11-17 stsp }
715 f4a881ce 2018-11-17 stsp } else {
716 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
717 f4a881ce 2018-11-17 stsp goto done;
718 f4a881ce 2018-11-17 stsp }
719 f4a881ce 2018-11-17 stsp
720 f4a881ce 2018-11-17 stsp tlen = strlen(GOT_TAG_TAG_TAGGER);
721 f4a881ce 2018-11-17 stsp if (strncmp(s, GOT_TAG_TAG_TAGGER, tlen) == 0) {
722 f4a881ce 2018-11-17 stsp char *p;
723 f4a881ce 2018-11-17 stsp size_t slen;
724 f4a881ce 2018-11-17 stsp
725 f4a881ce 2018-11-17 stsp remain -= tlen;
726 f4a881ce 2018-11-17 stsp if (remain <= 0) {
727 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
728 f4a881ce 2018-11-17 stsp goto done;
729 f4a881ce 2018-11-17 stsp }
730 f4a881ce 2018-11-17 stsp s += tlen;
731 f4a881ce 2018-11-17 stsp p = strchr(s, '\n');
732 f4a881ce 2018-11-17 stsp if (p == NULL) {
733 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
734 f4a881ce 2018-11-17 stsp goto done;
735 f4a881ce 2018-11-17 stsp }
736 f4a881ce 2018-11-17 stsp *p = '\0';
737 f4a881ce 2018-11-17 stsp slen = strlen(s);
738 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
739 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
740 f4a881ce 2018-11-17 stsp if (err)
741 f4a881ce 2018-11-17 stsp goto done;
742 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
743 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
744 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
745 f4a881ce 2018-11-17 stsp goto done;
746 f4a881ce 2018-11-17 stsp }
747 f4a881ce 2018-11-17 stsp s += slen + 1;
748 f4a881ce 2018-11-17 stsp remain -= slen + 1;
749 f4a881ce 2018-11-17 stsp if (remain <= 0) {
750 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
751 f4a881ce 2018-11-17 stsp goto done;
752 f4a881ce 2018-11-17 stsp }
753 f4a881ce 2018-11-17 stsp } else {
754 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
755 f4a881ce 2018-11-17 stsp goto done;
756 f4a881ce 2018-11-17 stsp }
757 f4a881ce 2018-11-17 stsp
758 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
759 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
760 f4a881ce 2018-11-17 stsp err = got_error_from_errno();
761 f4a881ce 2018-11-17 stsp goto done;
762 f4a881ce 2018-11-17 stsp }
763 f4a881ce 2018-11-17 stsp done:
764 f4a881ce 2018-11-17 stsp if (err) {
765 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
766 f4a881ce 2018-11-17 stsp *tag = NULL;
767 f4a881ce 2018-11-17 stsp }
768 f4a881ce 2018-11-17 stsp return err;
769 f4a881ce 2018-11-17 stsp }
770 f4a881ce 2018-11-17 stsp
771 f4a881ce 2018-11-17 stsp const struct got_error *
772 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
773 a440fac0 2018-09-06 stsp {
774 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
775 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
776 a440fac0 2018-09-06 stsp size_t n, total, remain;
777 a440fac0 2018-09-06 stsp uint8_t *buf;
778 a440fac0 2018-09-06 stsp
779 a440fac0 2018-09-06 stsp *outbuf = NULL;
780 a440fac0 2018-09-06 stsp *outlen = 0;
781 a440fac0 2018-09-06 stsp
782 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
783 a440fac0 2018-09-06 stsp if (buf == NULL)
784 a440fac0 2018-09-06 stsp return got_error_from_errno();
785 a440fac0 2018-09-06 stsp
786 a440fac0 2018-09-06 stsp remain = blocksize;
787 a440fac0 2018-09-06 stsp total = 0;
788 a440fac0 2018-09-06 stsp while (1) {
789 a440fac0 2018-09-06 stsp if (remain == 0) {
790 a440fac0 2018-09-06 stsp uint8_t *newbuf;
791 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
792 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
793 a440fac0 2018-09-06 stsp err = got_error_from_errno();
794 a440fac0 2018-09-06 stsp goto done;
795 a440fac0 2018-09-06 stsp }
796 a440fac0 2018-09-06 stsp buf = newbuf;
797 a440fac0 2018-09-06 stsp remain += blocksize;
798 a440fac0 2018-09-06 stsp }
799 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
800 a440fac0 2018-09-06 stsp if (n == 0) {
801 a440fac0 2018-09-06 stsp if (ferror(f)) {
802 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
803 a440fac0 2018-09-06 stsp goto done;
804 a440fac0 2018-09-06 stsp }
805 a440fac0 2018-09-06 stsp break; /* EOF */
806 a440fac0 2018-09-06 stsp }
807 a440fac0 2018-09-06 stsp remain -= n;
808 a440fac0 2018-09-06 stsp total += n;
809 a440fac0 2018-09-06 stsp };
810 a440fac0 2018-09-06 stsp
811 a440fac0 2018-09-06 stsp done:
812 a440fac0 2018-09-06 stsp if (err == NULL) {
813 a440fac0 2018-09-06 stsp *outbuf = buf;
814 a440fac0 2018-09-06 stsp *outlen = total;
815 a440fac0 2018-09-06 stsp } else
816 a440fac0 2018-09-06 stsp free(buf);
817 ad242220 2018-09-08 stsp return err;
818 a440fac0 2018-09-06 stsp }