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