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