Blame


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