Blame


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