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