Blame


1 a440fac0 2018-09-06 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 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 324d37e7 2019-05-11 stsp #include "got_path.h"
43 a440fac0 2018-09-06 stsp
44 a440fac0 2018-09-06 stsp #include "got_lib_sha1.h"
45 a440fac0 2018-09-06 stsp #include "got_lib_delta.h"
46 41fa1437 2018-11-05 stsp #include "got_lib_inflate.h"
47 41fa1437 2018-11-05 stsp #include "got_lib_object.h"
48 3022d272 2019-11-14 stsp #include "got_lib_object_parse.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
50 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
51 15a94983 2018-12-23 stsp #include "got_lib_privsep.h"
52 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
53 a440fac0 2018-09-06 stsp
54 a440fac0 2018-09-06 stsp #ifndef nitems
55 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 a440fac0 2018-09-06 stsp #endif
57 a440fac0 2018-09-06 stsp
58 f054b67a 2018-11-05 stsp int
59 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
60 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
61 f054b67a 2018-11-05 stsp {
62 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 f054b67a 2018-11-05 stsp }
64 f054b67a 2018-11-05 stsp
65 2ff12563 2018-09-15 stsp const struct got_error *
66 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
67 5df4932d 2018-11-05 stsp {
68 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
69 5df4932d 2018-11-05 stsp
70 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
71 5df4932d 2018-11-05 stsp if (*qid == NULL)
72 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
73 5df4932d 2018-11-05 stsp
74 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
75 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
76 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
77 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
78 5df4932d 2018-11-05 stsp *qid = NULL;
79 5df4932d 2018-11-05 stsp return err;
80 5df4932d 2018-11-05 stsp }
81 5df4932d 2018-11-05 stsp
82 5df4932d 2018-11-05 stsp return NULL;
83 5df4932d 2018-11-05 stsp }
84 5df4932d 2018-11-05 stsp
85 5df4932d 2018-11-05 stsp const struct got_error *
86 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
87 2ff12563 2018-09-15 stsp {
88 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
89 2ff12563 2018-09-15 stsp
90 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
91 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
92 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
93 2ff12563 2018-09-15 stsp
94 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
95 2ff12563 2018-09-15 stsp free(*outbuf);
96 2ff12563 2018-09-15 stsp *outbuf = NULL;
97 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
98 2ff12563 2018-09-15 stsp }
99 a440fac0 2018-09-06 stsp
100 2ff12563 2018-09-15 stsp return NULL;
101 2ff12563 2018-09-15 stsp }
102 2ff12563 2018-09-15 stsp
103 03fa71c8 2018-09-06 stsp void
104 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
105 03fa71c8 2018-09-06 stsp {
106 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
107 03fa71c8 2018-09-06 stsp obj->refcnt--;
108 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
109 03fa71c8 2018-09-06 stsp return;
110 03fa71c8 2018-09-06 stsp }
111 03fa71c8 2018-09-06 stsp
112 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
113 03fa71c8 2018-09-06 stsp struct got_delta *delta;
114 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
115 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
116 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
117 2256993b 2019-07-15 stsp free(delta);
118 03fa71c8 2018-09-06 stsp }
119 03fa71c8 2018-09-06 stsp }
120 03fa71c8 2018-09-06 stsp free(obj);
121 03fa71c8 2018-09-06 stsp }
122 03fa71c8 2018-09-06 stsp
123 03fa71c8 2018-09-06 stsp void
124 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
125 03fa71c8 2018-09-06 stsp {
126 03fa71c8 2018-09-06 stsp free(qid->id);
127 03fa71c8 2018-09-06 stsp free(qid);
128 1785f84a 2018-12-23 stsp }
129 1785f84a 2018-12-23 stsp
130 dd88155e 2019-06-29 stsp void
131 dd88155e 2019-06-29 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
132 dd88155e 2019-06-29 stsp {
133 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
134 dd88155e 2019-06-29 stsp
135 dd88155e 2019-06-29 stsp while (!SIMPLEQ_EMPTY(ids)) {
136 dd88155e 2019-06-29 stsp qid = SIMPLEQ_FIRST(ids);
137 dd88155e 2019-06-29 stsp SIMPLEQ_REMOVE_HEAD(ids, entry);
138 dd88155e 2019-06-29 stsp got_object_qid_free(qid);
139 dd88155e 2019-06-29 stsp }
140 dd88155e 2019-06-29 stsp }
141 dd88155e 2019-06-29 stsp
142 1785f84a 2018-12-23 stsp const struct got_error *
143 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
144 1785f84a 2018-12-23 stsp {
145 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
146 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
147 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
148 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
149 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
150 1785f84a 2018-12-23 stsp };
151 1785f84a 2018-12-23 stsp const int obj_types[] = {
152 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
153 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
154 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
155 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
156 1785f84a 2018-12-23 stsp };
157 1785f84a 2018-12-23 stsp int type = 0;
158 1785f84a 2018-12-23 stsp size_t size = 0, hdrlen = 0;
159 1785f84a 2018-12-23 stsp int i;
160 1785f84a 2018-12-23 stsp
161 1785f84a 2018-12-23 stsp *obj = NULL;
162 1785f84a 2018-12-23 stsp
163 9ef4ac16 2019-04-13 stsp hdrlen = strnlen(buf, len) + 1 /* '\0' */;
164 9ef4ac16 2019-04-13 stsp if (hdrlen > len)
165 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
166 1785f84a 2018-12-23 stsp
167 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
168 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
169 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
170 1785f84a 2018-12-23 stsp const char *errstr;
171 1785f84a 2018-12-23 stsp
172 ff2a4428 2019-03-19 stsp if (strncmp(buf, label, label_len) != 0)
173 1785f84a 2018-12-23 stsp continue;
174 1785f84a 2018-12-23 stsp
175 1785f84a 2018-12-23 stsp type = obj_types[i];
176 ff2a4428 2019-03-19 stsp if (len <= label_len)
177 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
178 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
179 1785f84a 2018-12-23 stsp if (errstr != NULL)
180 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
181 1785f84a 2018-12-23 stsp break;
182 1785f84a 2018-12-23 stsp }
183 1785f84a 2018-12-23 stsp
184 1785f84a 2018-12-23 stsp if (type == 0)
185 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
186 1785f84a 2018-12-23 stsp
187 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
188 1785f84a 2018-12-23 stsp if (*obj == NULL)
189 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
190 1785f84a 2018-12-23 stsp (*obj)->type = type;
191 1785f84a 2018-12-23 stsp (*obj)->hdrlen = hdrlen;
192 1785f84a 2018-12-23 stsp (*obj)->size = size;
193 1785f84a 2018-12-23 stsp return NULL;
194 1785f84a 2018-12-23 stsp }
195 1785f84a 2018-12-23 stsp
196 1785f84a 2018-12-23 stsp const struct got_error *
197 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
198 1785f84a 2018-12-23 stsp {
199 1785f84a 2018-12-23 stsp const struct got_error *err;
200 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
201 1785f84a 2018-12-23 stsp char *buf;
202 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
203 1785f84a 2018-12-23 stsp size_t outlen, totlen;
204 1785f84a 2018-12-23 stsp int nbuf = 1;
205 1785f84a 2018-12-23 stsp
206 1785f84a 2018-12-23 stsp *obj = NULL;
207 1785f84a 2018-12-23 stsp
208 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
209 1785f84a 2018-12-23 stsp if (buf == NULL)
210 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
211 1785f84a 2018-12-23 stsp
212 1785f84a 2018-12-23 stsp err = got_inflate_init(&zb, buf, zbsize);
213 1785f84a 2018-12-23 stsp if (err)
214 1785f84a 2018-12-23 stsp return err;
215 1785f84a 2018-12-23 stsp
216 1785f84a 2018-12-23 stsp totlen = 0;
217 1785f84a 2018-12-23 stsp do {
218 1785f84a 2018-12-23 stsp err = got_inflate_read_fd(&zb, fd, &outlen);
219 1785f84a 2018-12-23 stsp if (err)
220 1785f84a 2018-12-23 stsp goto done;
221 1785f84a 2018-12-23 stsp if (outlen == 0)
222 1785f84a 2018-12-23 stsp break;
223 1785f84a 2018-12-23 stsp totlen += outlen;
224 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
225 1785f84a 2018-12-23 stsp char *newbuf;
226 1785f84a 2018-12-23 stsp nbuf++;
227 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
228 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
229 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
230 1785f84a 2018-12-23 stsp goto done;
231 1785f84a 2018-12-23 stsp }
232 1785f84a 2018-12-23 stsp buf = newbuf;
233 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
234 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
235 1785f84a 2018-12-23 stsp }
236 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
237 1785f84a 2018-12-23 stsp
238 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
239 1785f84a 2018-12-23 stsp done:
240 1785f84a 2018-12-23 stsp free(buf);
241 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
242 1785f84a 2018-12-23 stsp return err;
243 876c234b 2018-09-10 stsp }
244 876c234b 2018-09-10 stsp
245 a440fac0 2018-09-06 stsp struct got_commit_object *
246 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
247 a440fac0 2018-09-06 stsp {
248 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
249 a440fac0 2018-09-06 stsp
250 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
251 a440fac0 2018-09-06 stsp if (commit == NULL)
252 a440fac0 2018-09-06 stsp return NULL;
253 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
254 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
255 a440fac0 2018-09-06 stsp free(commit);
256 a440fac0 2018-09-06 stsp return NULL;
257 a440fac0 2018-09-06 stsp }
258 a440fac0 2018-09-06 stsp
259 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
260 a440fac0 2018-09-06 stsp
261 a440fac0 2018-09-06 stsp return commit;
262 a440fac0 2018-09-06 stsp }
263 a440fac0 2018-09-06 stsp
264 a440fac0 2018-09-06 stsp const struct got_error *
265 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
266 a440fac0 2018-09-06 stsp const char *id_str)
267 a440fac0 2018-09-06 stsp {
268 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
269 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
270 a440fac0 2018-09-06 stsp
271 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
272 5df4932d 2018-11-05 stsp if (err)
273 7762fe12 2018-11-05 stsp return err;
274 a440fac0 2018-09-06 stsp
275 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
276 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
277 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
278 a440fac0 2018-09-06 stsp return err;
279 a440fac0 2018-09-06 stsp }
280 a440fac0 2018-09-06 stsp
281 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
282 a440fac0 2018-09-06 stsp commit->nparents++;
283 a440fac0 2018-09-06 stsp
284 a440fac0 2018-09-06 stsp return NULL;
285 a440fac0 2018-09-06 stsp }
286 a440fac0 2018-09-06 stsp
287 a440fac0 2018-09-06 stsp static const struct got_error *
288 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
289 a440fac0 2018-09-06 stsp {
290 a440fac0 2018-09-06 stsp int sign = 1;
291 a440fac0 2018-09-06 stsp const char *p = tzstr;
292 a440fac0 2018-09-06 stsp time_t h, m;
293 a440fac0 2018-09-06 stsp
294 a440fac0 2018-09-06 stsp *gmtoff = 0;
295 a440fac0 2018-09-06 stsp
296 a440fac0 2018-09-06 stsp if (*p == '-')
297 a440fac0 2018-09-06 stsp sign = -1;
298 a440fac0 2018-09-06 stsp else if (*p != '+')
299 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
300 a440fac0 2018-09-06 stsp p++;
301 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
302 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
303 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
304 a440fac0 2018-09-06 stsp
305 a440fac0 2018-09-06 stsp p += 2;
306 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
307 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
308 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
309 a440fac0 2018-09-06 stsp
310 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
311 a440fac0 2018-09-06 stsp return NULL;
312 a440fac0 2018-09-06 stsp }
313 a440fac0 2018-09-06 stsp
314 a440fac0 2018-09-06 stsp static const struct got_error *
315 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
316 a440fac0 2018-09-06 stsp {
317 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
318 a440fac0 2018-09-06 stsp const char *errstr;
319 a440fac0 2018-09-06 stsp char *space, *tzstr;
320 a440fac0 2018-09-06 stsp
321 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
322 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
323 a440fac0 2018-09-06 stsp if (space == NULL)
324 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
325 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
326 a440fac0 2018-09-06 stsp if (tzstr == NULL)
327 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
328 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
329 a440fac0 2018-09-06 stsp free(tzstr);
330 a440fac0 2018-09-06 stsp if (err)
331 a440fac0 2018-09-06 stsp return err;
332 a440fac0 2018-09-06 stsp *space = '\0';
333 a440fac0 2018-09-06 stsp
334 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
335 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
336 a440fac0 2018-09-06 stsp if (space == NULL)
337 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
338 a440fac0 2018-09-06 stsp
339 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
340 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
341 a440fac0 2018-09-06 stsp if (errstr)
342 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
343 a440fac0 2018-09-06 stsp
344 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
345 a440fac0 2018-09-06 stsp *space = '\0';
346 a440fac0 2018-09-06 stsp
347 a440fac0 2018-09-06 stsp return NULL;
348 a440fac0 2018-09-06 stsp }
349 a440fac0 2018-09-06 stsp
350 03fa71c8 2018-09-06 stsp void
351 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
352 03fa71c8 2018-09-06 stsp {
353 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
354 03fa71c8 2018-09-06 stsp commit->refcnt--;
355 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
356 03fa71c8 2018-09-06 stsp return;
357 03fa71c8 2018-09-06 stsp }
358 03fa71c8 2018-09-06 stsp
359 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
360 03fa71c8 2018-09-06 stsp free(commit->tree_id);
361 03fa71c8 2018-09-06 stsp free(commit->author);
362 03fa71c8 2018-09-06 stsp free(commit->committer);
363 03fa71c8 2018-09-06 stsp free(commit->logmsg);
364 03fa71c8 2018-09-06 stsp free(commit);
365 45d799e2 2018-12-23 stsp }
366 45d799e2 2018-12-23 stsp
367 45d799e2 2018-12-23 stsp struct got_object_id *
368 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
369 45d799e2 2018-12-23 stsp {
370 45d799e2 2018-12-23 stsp return commit->tree_id;
371 45d799e2 2018-12-23 stsp }
372 45d799e2 2018-12-23 stsp
373 45d799e2 2018-12-23 stsp int
374 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
375 45d799e2 2018-12-23 stsp {
376 45d799e2 2018-12-23 stsp return commit->nparents;
377 03fa71c8 2018-09-06 stsp }
378 03fa71c8 2018-09-06 stsp
379 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
380 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
381 45d799e2 2018-12-23 stsp {
382 45d799e2 2018-12-23 stsp return &commit->parent_ids;
383 45d799e2 2018-12-23 stsp }
384 45d799e2 2018-12-23 stsp
385 45d799e2 2018-12-23 stsp const char *
386 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
387 45d799e2 2018-12-23 stsp {
388 45d799e2 2018-12-23 stsp return commit->author;
389 45d799e2 2018-12-23 stsp }
390 45d799e2 2018-12-23 stsp
391 45d799e2 2018-12-23 stsp time_t
392 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
393 45d799e2 2018-12-23 stsp {
394 45d799e2 2018-12-23 stsp return commit->author_time;
395 45d799e2 2018-12-23 stsp }
396 45d799e2 2018-12-23 stsp
397 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
398 45d799e2 2018-12-23 stsp {
399 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
400 45d799e2 2018-12-23 stsp }
401 45d799e2 2018-12-23 stsp
402 45d799e2 2018-12-23 stsp const char *
403 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
404 45d799e2 2018-12-23 stsp {
405 45d799e2 2018-12-23 stsp return commit->committer;
406 45d799e2 2018-12-23 stsp }
407 45d799e2 2018-12-23 stsp
408 45d799e2 2018-12-23 stsp time_t
409 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
410 45d799e2 2018-12-23 stsp {
411 45d799e2 2018-12-23 stsp return commit->committer_time;
412 45d799e2 2018-12-23 stsp }
413 45d799e2 2018-12-23 stsp
414 45d799e2 2018-12-23 stsp time_t
415 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
416 45d799e2 2018-12-23 stsp {
417 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
418 45d799e2 2018-12-23 stsp }
419 5943eee2 2019-08-13 stsp
420 5943eee2 2019-08-13 stsp const struct got_error *
421 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
422 45d799e2 2018-12-23 stsp {
423 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
424 5943eee2 2019-08-13 stsp char *msg0, *msg, *line, *s;
425 5943eee2 2019-08-13 stsp size_t len;
426 13555e04 2019-09-28 semarie int headers = 1;
427 5943eee2 2019-08-13 stsp
428 5943eee2 2019-08-13 stsp *logmsg = NULL;
429 5943eee2 2019-08-13 stsp
430 5943eee2 2019-08-13 stsp msg0 = strdup(commit->logmsg);
431 5943eee2 2019-08-13 stsp if (msg0 == NULL)
432 5943eee2 2019-08-13 stsp return got_error_from_errno("strdup");
433 5943eee2 2019-08-13 stsp
434 13555e04 2019-09-28 semarie /* Copy log message line by line to strip out unusual headers... */
435 5943eee2 2019-08-13 stsp msg = msg0;
436 5943eee2 2019-08-13 stsp do {
437 13555e04 2019-09-28 semarie if ((line = strsep(&msg, "\n")) == NULL)
438 13555e04 2019-09-28 semarie break;
439 5943eee2 2019-08-13 stsp
440 13555e04 2019-09-28 semarie if (headers == 1) {
441 13555e04 2019-09-28 semarie if (line[0] != '\0' &&
442 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_TREE,
443 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
444 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_AUTHOR,
445 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
446 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_PARENT,
447 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
448 13555e04 2019-09-28 semarie strncmp(line, GOT_COMMIT_LABEL_COMMITTER,
449 13555e04 2019-09-28 semarie strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
450 5943eee2 2019-08-13 stsp continue;
451 13555e04 2019-09-28 semarie
452 13555e04 2019-09-28 semarie if (line[0] == '\0')
453 13555e04 2019-09-28 semarie headers = 0;
454 5943eee2 2019-08-13 stsp }
455 13555e04 2019-09-28 semarie
456 13555e04 2019-09-28 semarie if (asprintf(&s, "%s%s\n",
457 13555e04 2019-09-28 semarie *logmsg ? *logmsg : "", line) == -1) {
458 13555e04 2019-09-28 semarie err = got_error_from_errno("asprintf");
459 13555e04 2019-09-28 semarie goto done;
460 13555e04 2019-09-28 semarie }
461 13555e04 2019-09-28 semarie free(*logmsg);
462 13555e04 2019-09-28 semarie *logmsg = s;
463 13555e04 2019-09-28 semarie
464 5943eee2 2019-08-13 stsp } while (line);
465 5943eee2 2019-08-13 stsp
466 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
467 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
468 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
469 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
470 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
471 5943eee2 2019-08-13 stsp len--;
472 5943eee2 2019-08-13 stsp }
473 5943eee2 2019-08-13 stsp done:
474 5943eee2 2019-08-13 stsp free(msg0);
475 5943eee2 2019-08-13 stsp if (err) {
476 5943eee2 2019-08-13 stsp free(*logmsg);
477 5943eee2 2019-08-13 stsp *logmsg = NULL;
478 5943eee2 2019-08-13 stsp }
479 5943eee2 2019-08-13 stsp return err;
480 24ea5512 2019-08-22 stsp }
481 24ea5512 2019-08-22 stsp
482 24ea5512 2019-08-22 stsp const char *
483 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
484 24ea5512 2019-08-22 stsp {
485 24ea5512 2019-08-22 stsp return commit->logmsg;
486 45d799e2 2018-12-23 stsp }
487 45d799e2 2018-12-23 stsp
488 a440fac0 2018-09-06 stsp const struct got_error *
489 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
490 5e0b25c4 2018-12-24 stsp size_t len)
491 a440fac0 2018-09-06 stsp {
492 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
493 a440fac0 2018-09-06 stsp char *s = buf;
494 ff2a4428 2019-03-19 stsp size_t label_len;
495 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
496 4793d91b 2019-09-22 stsp
497 4793d91b 2019-09-22 stsp if (remain == 0)
498 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
499 230a42bd 2019-05-11 jcs
500 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
501 a440fac0 2018-09-06 stsp if (*commit == NULL)
502 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
503 a440fac0 2018-09-06 stsp
504 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
505 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
506 ff2a4428 2019-03-19 stsp remain -= label_len;
507 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
508 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 a440fac0 2018-09-06 stsp goto done;
510 a440fac0 2018-09-06 stsp }
511 ff2a4428 2019-03-19 stsp s += label_len;
512 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
513 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
514 a440fac0 2018-09-06 stsp goto done;
515 a440fac0 2018-09-06 stsp }
516 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
517 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
518 a440fac0 2018-09-06 stsp } else {
519 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
520 a440fac0 2018-09-06 stsp goto done;
521 a440fac0 2018-09-06 stsp }
522 a440fac0 2018-09-06 stsp
523 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
524 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
525 ff2a4428 2019-03-19 stsp remain -= label_len;
526 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
527 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
528 a440fac0 2018-09-06 stsp goto done;
529 a440fac0 2018-09-06 stsp }
530 ff2a4428 2019-03-19 stsp s += label_len;
531 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
532 a440fac0 2018-09-06 stsp if (err)
533 a440fac0 2018-09-06 stsp goto done;
534 a440fac0 2018-09-06 stsp
535 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
536 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
537 a440fac0 2018-09-06 stsp }
538 a440fac0 2018-09-06 stsp
539 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
540 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
541 a440fac0 2018-09-06 stsp char *p;
542 a440fac0 2018-09-06 stsp size_t slen;
543 a440fac0 2018-09-06 stsp
544 ff2a4428 2019-03-19 stsp remain -= label_len;
545 a440fac0 2018-09-06 stsp if (remain <= 0) {
546 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
547 a440fac0 2018-09-06 stsp goto done;
548 a440fac0 2018-09-06 stsp }
549 ff2a4428 2019-03-19 stsp s += label_len;
550 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
551 a440fac0 2018-09-06 stsp if (p == NULL) {
552 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
553 a440fac0 2018-09-06 stsp goto done;
554 a440fac0 2018-09-06 stsp }
555 a440fac0 2018-09-06 stsp *p = '\0';
556 a440fac0 2018-09-06 stsp slen = strlen(s);
557 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
558 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
559 a440fac0 2018-09-06 stsp if (err)
560 a440fac0 2018-09-06 stsp goto done;
561 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
562 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
563 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
564 a440fac0 2018-09-06 stsp goto done;
565 a440fac0 2018-09-06 stsp }
566 a440fac0 2018-09-06 stsp s += slen + 1;
567 a440fac0 2018-09-06 stsp remain -= slen + 1;
568 a440fac0 2018-09-06 stsp }
569 a440fac0 2018-09-06 stsp
570 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
571 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
572 a440fac0 2018-09-06 stsp char *p;
573 a440fac0 2018-09-06 stsp size_t slen;
574 a440fac0 2018-09-06 stsp
575 ff2a4428 2019-03-19 stsp remain -= label_len;
576 a440fac0 2018-09-06 stsp if (remain <= 0) {
577 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
578 a440fac0 2018-09-06 stsp goto done;
579 a440fac0 2018-09-06 stsp }
580 ff2a4428 2019-03-19 stsp s += label_len;
581 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
582 a440fac0 2018-09-06 stsp if (p == NULL) {
583 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 a440fac0 2018-09-06 stsp goto done;
585 a440fac0 2018-09-06 stsp }
586 a440fac0 2018-09-06 stsp *p = '\0';
587 a440fac0 2018-09-06 stsp slen = strlen(s);
588 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
589 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
590 a440fac0 2018-09-06 stsp if (err)
591 a440fac0 2018-09-06 stsp goto done;
592 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
593 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
594 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
595 a440fac0 2018-09-06 stsp goto done;
596 a440fac0 2018-09-06 stsp }
597 a440fac0 2018-09-06 stsp s += slen + 1;
598 a440fac0 2018-09-06 stsp remain -= slen + 1;
599 a440fac0 2018-09-06 stsp }
600 a440fac0 2018-09-06 stsp
601 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
602 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
603 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
604 a440fac0 2018-09-06 stsp goto done;
605 a440fac0 2018-09-06 stsp }
606 a440fac0 2018-09-06 stsp done:
607 a440fac0 2018-09-06 stsp if (err) {
608 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
609 a440fac0 2018-09-06 stsp *commit = NULL;
610 a440fac0 2018-09-06 stsp }
611 a440fac0 2018-09-06 stsp return err;
612 ed175427 2019-05-09 stsp }
613 ed175427 2019-05-09 stsp
614 ed175427 2019-05-09 stsp void
615 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
616 ed175427 2019-05-09 stsp {
617 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
618 03fa71c8 2018-09-06 stsp tree->refcnt--;
619 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
620 03fa71c8 2018-09-06 stsp return;
621 03fa71c8 2018-09-06 stsp }
622 03fa71c8 2018-09-06 stsp
623 56e0773d 2019-11-28 stsp free(tree->entries);
624 03fa71c8 2018-09-06 stsp free(tree);
625 03fa71c8 2018-09-06 stsp }
626 03fa71c8 2018-09-06 stsp
627 a440fac0 2018-09-06 stsp static const struct got_error *
628 3022d272 2019-11-14 stsp parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
629 3022d272 2019-11-14 stsp size_t *elen, char *buf,
630 a440fac0 2018-09-06 stsp size_t maxlen)
631 a440fac0 2018-09-06 stsp {
632 8914529d 2019-04-13 stsp char *p, *space;
633 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
634 a0de39f3 2019-08-09 stsp
635 3022d272 2019-11-14 stsp *name = NULL;
636 a0de39f3 2019-08-09 stsp *elen = 0;
637 a440fac0 2018-09-06 stsp
638 3022d272 2019-11-14 stsp *pte = malloc(sizeof(**pte));
639 3022d272 2019-11-14 stsp if (*pte == NULL)
640 3022d272 2019-11-14 stsp return got_error_from_errno("malloc");
641 a440fac0 2018-09-06 stsp
642 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
643 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
644 3022d272 2019-11-14 stsp free(*pte);
645 3022d272 2019-11-14 stsp *pte = NULL;
646 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
647 a440fac0 2018-09-06 stsp }
648 a440fac0 2018-09-06 stsp
649 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
650 8914529d 2019-04-13 stsp if (space == NULL || space <= buf) {
651 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
652 3022d272 2019-11-14 stsp free(*pte);
653 3022d272 2019-11-14 stsp *pte = NULL;
654 a440fac0 2018-09-06 stsp return err;
655 a440fac0 2018-09-06 stsp }
656 3022d272 2019-11-14 stsp (*pte)->mode = 0;
657 8914529d 2019-04-13 stsp p = buf;
658 8914529d 2019-04-13 stsp while (p < space) {
659 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
660 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
661 a440fac0 2018-09-06 stsp goto done;
662 a440fac0 2018-09-06 stsp }
663 3022d272 2019-11-14 stsp (*pte)->mode <<= 3;
664 3022d272 2019-11-14 stsp (*pte)->mode |= *p - '0';
665 a440fac0 2018-09-06 stsp p++;
666 a440fac0 2018-09-06 stsp }
667 a440fac0 2018-09-06 stsp
668 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
669 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 a440fac0 2018-09-06 stsp goto done;
671 a440fac0 2018-09-06 stsp }
672 3022d272 2019-11-14 stsp *name = space + 1;
673 68bf1b1e 2018-11-07 stsp buf += *elen;
674 3022d272 2019-11-14 stsp (*pte)->id = buf;
675 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
676 a440fac0 2018-09-06 stsp done:
677 a440fac0 2018-09-06 stsp if (err) {
678 3022d272 2019-11-14 stsp free(*pte);
679 3022d272 2019-11-14 stsp *pte = NULL;
680 a440fac0 2018-09-06 stsp }
681 a440fac0 2018-09-06 stsp return err;
682 a440fac0 2018-09-06 stsp }
683 a440fac0 2018-09-06 stsp
684 a440fac0 2018-09-06 stsp const struct got_error *
685 3022d272 2019-11-14 stsp got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
686 3022d272 2019-11-14 stsp uint8_t *buf, size_t len)
687 a440fac0 2018-09-06 stsp {
688 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
689 a440fac0 2018-09-06 stsp size_t remain = len;
690 f5d3d7af 2019-02-05 stsp
691 3022d272 2019-11-14 stsp *nentries = 0;
692 db1d3576 2019-10-04 stsp if (remain == 0)
693 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
694 db1d3576 2019-10-04 stsp
695 a440fac0 2018-09-06 stsp while (remain > 0) {
696 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte;
697 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
698 3022d272 2019-11-14 stsp const char *name;
699 a440fac0 2018-09-06 stsp size_t elen;
700 a440fac0 2018-09-06 stsp
701 3022d272 2019-11-14 stsp err = parse_tree_entry(&pte, &name, &elen, buf, remain);
702 a440fac0 2018-09-06 stsp if (err)
703 f5d3d7af 2019-02-05 stsp goto done;
704 3022d272 2019-11-14 stsp err = got_pathlist_insert(&new, entries, name, pte);
705 f5d3d7af 2019-02-05 stsp if (err)
706 f5d3d7af 2019-02-05 stsp goto done;
707 f5d3d7af 2019-02-05 stsp if (new == NULL) {
708 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
709 f5d3d7af 2019-02-05 stsp goto done;
710 f5d3d7af 2019-02-05 stsp }
711 a440fac0 2018-09-06 stsp buf += elen;
712 a440fac0 2018-09-06 stsp remain -= elen;
713 3022d272 2019-11-14 stsp (*nentries)++;
714 a440fac0 2018-09-06 stsp }
715 a440fac0 2018-09-06 stsp
716 a440fac0 2018-09-06 stsp if (remain != 0) {
717 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
718 f5d3d7af 2019-02-05 stsp goto done;
719 a440fac0 2018-09-06 stsp }
720 3022d272 2019-11-14 stsp done:
721 3022d272 2019-11-14 stsp if (err) {
722 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(entries);
723 3022d272 2019-11-14 stsp *nentries = 0;
724 f5d3d7af 2019-02-05 stsp }
725 f5d3d7af 2019-02-05 stsp return err;
726 b64b1f95 2020-01-06 stsp }
727 b64b1f95 2020-01-06 stsp
728 b64b1f95 2020-01-06 stsp void
729 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
730 b64b1f95 2020-01-06 stsp {
731 b64b1f95 2020-01-06 stsp struct got_pathlist_entry *pe;
732 b64b1f95 2020-01-06 stsp
733 b64b1f95 2020-01-06 stsp TAILQ_FOREACH(pe, entries, entry) {
734 b64b1f95 2020-01-06 stsp struct got_parsed_tree_entry *pte = pe->data;
735 b64b1f95 2020-01-06 stsp free(pte);
736 b64b1f95 2020-01-06 stsp }
737 b64b1f95 2020-01-06 stsp got_pathlist_free(entries);
738 a440fac0 2018-09-06 stsp }
739 a440fac0 2018-09-06 stsp
740 f4a881ce 2018-11-17 stsp void
741 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
742 f4a881ce 2018-11-17 stsp {
743 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
744 ca0d469c 2019-08-13 stsp tag->refcnt--;
745 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
746 ca0d469c 2019-08-13 stsp return;
747 ca0d469c 2019-08-13 stsp }
748 ca0d469c 2019-08-13 stsp
749 f4a881ce 2018-11-17 stsp free(tag->tag);
750 f4a881ce 2018-11-17 stsp free(tag->tagger);
751 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
752 f4a881ce 2018-11-17 stsp free(tag);
753 f4a881ce 2018-11-17 stsp }
754 f4a881ce 2018-11-17 stsp
755 ad242220 2018-09-08 stsp const struct got_error *
756 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
757 f4a881ce 2018-11-17 stsp {
758 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
759 f4a881ce 2018-11-17 stsp size_t remain = len;
760 f4a881ce 2018-11-17 stsp char *s = buf;
761 ff2a4428 2019-03-19 stsp size_t label_len;
762 4793d91b 2019-09-22 stsp
763 4793d91b 2019-09-22 stsp if (remain == 0)
764 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
765 f4a881ce 2018-11-17 stsp
766 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
767 f4a881ce 2018-11-17 stsp if (*tag == NULL)
768 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
769 f4a881ce 2018-11-17 stsp
770 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
771 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
772 ff2a4428 2019-03-19 stsp remain -= label_len;
773 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
774 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
775 f4a881ce 2018-11-17 stsp goto done;
776 f4a881ce 2018-11-17 stsp }
777 ff2a4428 2019-03-19 stsp s += label_len;
778 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
779 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
780 f4a881ce 2018-11-17 stsp goto done;
781 f4a881ce 2018-11-17 stsp }
782 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
783 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
784 f4a881ce 2018-11-17 stsp } else {
785 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
786 f4a881ce 2018-11-17 stsp goto done;
787 f4a881ce 2018-11-17 stsp }
788 f4a881ce 2018-11-17 stsp
789 f4a881ce 2018-11-17 stsp if (remain <= 0) {
790 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
791 f4a881ce 2018-11-17 stsp goto done;
792 f4a881ce 2018-11-17 stsp }
793 f4a881ce 2018-11-17 stsp
794 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
795 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
796 ff2a4428 2019-03-19 stsp remain -= label_len;
797 f4a881ce 2018-11-17 stsp if (remain <= 0) {
798 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 f4a881ce 2018-11-17 stsp goto done;
800 f4a881ce 2018-11-17 stsp }
801 ff2a4428 2019-03-19 stsp s += label_len;
802 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
803 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
804 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
805 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
806 ff2a4428 2019-03-19 stsp s += label_len;
807 ff2a4428 2019-03-19 stsp remain -= label_len;
808 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
809 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
810 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
811 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
812 ff2a4428 2019-03-19 stsp s += label_len;
813 ff2a4428 2019-03-19 stsp remain -= label_len;
814 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
815 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
816 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
817 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
818 ff2a4428 2019-03-19 stsp s += label_len;
819 ff2a4428 2019-03-19 stsp remain -= label_len;
820 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
821 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
822 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
823 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
824 ff2a4428 2019-03-19 stsp s += label_len;
825 ff2a4428 2019-03-19 stsp remain -= label_len;
826 f4a881ce 2018-11-17 stsp } else {
827 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
828 f4a881ce 2018-11-17 stsp goto done;
829 f4a881ce 2018-11-17 stsp }
830 f4a881ce 2018-11-17 stsp
831 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
832 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
833 f4a881ce 2018-11-17 stsp goto done;
834 f4a881ce 2018-11-17 stsp }
835 f4a881ce 2018-11-17 stsp s++;
836 f4a881ce 2018-11-17 stsp remain--;
837 f4a881ce 2018-11-17 stsp if (remain <= 0) {
838 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
839 f4a881ce 2018-11-17 stsp goto done;
840 f4a881ce 2018-11-17 stsp }
841 f4a881ce 2018-11-17 stsp } else {
842 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
843 f4a881ce 2018-11-17 stsp goto done;
844 f4a881ce 2018-11-17 stsp }
845 f4a881ce 2018-11-17 stsp
846 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
847 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
848 f4a881ce 2018-11-17 stsp char *p;
849 f4a881ce 2018-11-17 stsp size_t slen;
850 ff2a4428 2019-03-19 stsp remain -= label_len;
851 f4a881ce 2018-11-17 stsp if (remain <= 0) {
852 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 f4a881ce 2018-11-17 stsp goto done;
854 f4a881ce 2018-11-17 stsp }
855 ff2a4428 2019-03-19 stsp s += label_len;
856 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
857 f4a881ce 2018-11-17 stsp if (p == NULL) {
858 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
859 f4a881ce 2018-11-17 stsp goto done;
860 f4a881ce 2018-11-17 stsp }
861 f4a881ce 2018-11-17 stsp *p = '\0';
862 f4a881ce 2018-11-17 stsp slen = strlen(s);
863 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
864 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
865 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
866 f4a881ce 2018-11-17 stsp goto done;
867 f4a881ce 2018-11-17 stsp }
868 f4a881ce 2018-11-17 stsp s += slen + 1;
869 f4a881ce 2018-11-17 stsp remain -= slen + 1;
870 f4a881ce 2018-11-17 stsp if (remain <= 0) {
871 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
872 f4a881ce 2018-11-17 stsp goto done;
873 f4a881ce 2018-11-17 stsp }
874 f4a881ce 2018-11-17 stsp } else {
875 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
876 f4a881ce 2018-11-17 stsp goto done;
877 f4a881ce 2018-11-17 stsp }
878 f4a881ce 2018-11-17 stsp
879 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
880 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
881 f4a881ce 2018-11-17 stsp char *p;
882 f4a881ce 2018-11-17 stsp size_t slen;
883 f4a881ce 2018-11-17 stsp
884 ff2a4428 2019-03-19 stsp remain -= label_len;
885 f4a881ce 2018-11-17 stsp if (remain <= 0) {
886 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 f4a881ce 2018-11-17 stsp goto done;
888 f4a881ce 2018-11-17 stsp }
889 ff2a4428 2019-03-19 stsp s += label_len;
890 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
891 f4a881ce 2018-11-17 stsp if (p == NULL) {
892 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
893 f4a881ce 2018-11-17 stsp goto done;
894 f4a881ce 2018-11-17 stsp }
895 f4a881ce 2018-11-17 stsp *p = '\0';
896 f4a881ce 2018-11-17 stsp slen = strlen(s);
897 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
898 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
899 f4a881ce 2018-11-17 stsp if (err)
900 f4a881ce 2018-11-17 stsp goto done;
901 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
902 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
903 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
904 f4a881ce 2018-11-17 stsp goto done;
905 f4a881ce 2018-11-17 stsp }
906 f4a881ce 2018-11-17 stsp s += slen + 1;
907 f4a881ce 2018-11-17 stsp remain -= slen + 1;
908 f4a881ce 2018-11-17 stsp if (remain <= 0) {
909 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
910 f4a881ce 2018-11-17 stsp goto done;
911 f4a881ce 2018-11-17 stsp }
912 f4a881ce 2018-11-17 stsp } else {
913 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
914 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
915 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
916 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
917 e0e55b50 2019-02-01 stsp goto done;
918 e0e55b50 2019-02-01 stsp }
919 f4a881ce 2018-11-17 stsp }
920 f4a881ce 2018-11-17 stsp
921 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
922 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
923 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
924 f4a881ce 2018-11-17 stsp goto done;
925 f4a881ce 2018-11-17 stsp }
926 f4a881ce 2018-11-17 stsp done:
927 f4a881ce 2018-11-17 stsp if (err) {
928 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
929 f4a881ce 2018-11-17 stsp *tag = NULL;
930 f4a881ce 2018-11-17 stsp }
931 f4a881ce 2018-11-17 stsp return err;
932 f4a881ce 2018-11-17 stsp }
933 f4a881ce 2018-11-17 stsp
934 f4a881ce 2018-11-17 stsp const struct got_error *
935 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
936 a440fac0 2018-09-06 stsp {
937 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
938 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
939 a440fac0 2018-09-06 stsp size_t n, total, remain;
940 a440fac0 2018-09-06 stsp uint8_t *buf;
941 a440fac0 2018-09-06 stsp
942 a440fac0 2018-09-06 stsp *outbuf = NULL;
943 a440fac0 2018-09-06 stsp *outlen = 0;
944 a440fac0 2018-09-06 stsp
945 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
946 a440fac0 2018-09-06 stsp if (buf == NULL)
947 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
948 a440fac0 2018-09-06 stsp
949 a440fac0 2018-09-06 stsp remain = blocksize;
950 a440fac0 2018-09-06 stsp total = 0;
951 656b1f76 2019-05-11 jcs for (;;) {
952 a440fac0 2018-09-06 stsp if (remain == 0) {
953 a440fac0 2018-09-06 stsp uint8_t *newbuf;
954 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
955 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
956 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
957 a440fac0 2018-09-06 stsp goto done;
958 a440fac0 2018-09-06 stsp }
959 a440fac0 2018-09-06 stsp buf = newbuf;
960 a440fac0 2018-09-06 stsp remain += blocksize;
961 a440fac0 2018-09-06 stsp }
962 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
963 a440fac0 2018-09-06 stsp if (n == 0) {
964 a440fac0 2018-09-06 stsp if (ferror(f)) {
965 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
966 a440fac0 2018-09-06 stsp goto done;
967 a440fac0 2018-09-06 stsp }
968 a440fac0 2018-09-06 stsp break; /* EOF */
969 a440fac0 2018-09-06 stsp }
970 a440fac0 2018-09-06 stsp remain -= n;
971 a440fac0 2018-09-06 stsp total += n;
972 a440fac0 2018-09-06 stsp };
973 a440fac0 2018-09-06 stsp
974 a440fac0 2018-09-06 stsp done:
975 a440fac0 2018-09-06 stsp if (err == NULL) {
976 a440fac0 2018-09-06 stsp *outbuf = buf;
977 a440fac0 2018-09-06 stsp *outlen = total;
978 a440fac0 2018-09-06 stsp } else
979 a440fac0 2018-09-06 stsp free(buf);
980 ad242220 2018-09-08 stsp return err;
981 a440fac0 2018-09-06 stsp }