Blame


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