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