Blame


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