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 close(fd);
286 3efd8e31 2022-10-23 thomas goto done;
287 3efd8e31 2022-10-23 thomas }
288 3efd8e31 2022-10-23 thomas err = got_inflate_to_fd(&len, f, &csum, outfd);
289 3efd8e31 2022-10-23 thomas }
290 3efd8e31 2022-10-23 thomas if (err)
291 3efd8e31 2022-10-23 thomas goto done;
292 3efd8e31 2022-10-23 thomas
293 3efd8e31 2022-10-23 thomas if (len < obj->hdrlen || len != obj->hdrlen + obj->size) {
294 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_OBJ_HDR);
295 3efd8e31 2022-10-23 thomas goto done;
296 3efd8e31 2022-10-23 thomas }
297 3efd8e31 2022-10-23 thomas
298 b16893ba 2023-02-24 thomas got_hash_final_object_id(&ctx, &id);
299 b16893ba 2023-02-24 thomas if (got_object_id_cmp(expected_id, &id) != 0) {
300 dc43cdc9 2023-02-07 thomas err = got_error_checksum(expected_id);
301 3efd8e31 2022-10-23 thomas goto done;
302 3efd8e31 2022-10-23 thomas }
303 3efd8e31 2022-10-23 thomas
304 3efd8e31 2022-10-23 thomas *size = obj->size;
305 3efd8e31 2022-10-23 thomas *hdrlen = obj->hdrlen;
306 3efd8e31 2022-10-23 thomas done:
307 3efd8e31 2022-10-23 thomas got_object_close(obj);
308 3efd8e31 2022-10-23 thomas if (f && fclose(f) == EOF && err == NULL)
309 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
310 3efd8e31 2022-10-23 thomas return err;
311 3efd8e31 2022-10-23 thomas }
312 3efd8e31 2022-10-23 thomas
313 a440fac0 2018-09-06 stsp struct got_commit_object *
314 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
315 a440fac0 2018-09-06 stsp {
316 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
317 a440fac0 2018-09-06 stsp
318 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
319 a440fac0 2018-09-06 stsp if (commit == NULL)
320 a440fac0 2018-09-06 stsp return NULL;
321 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
322 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
323 a440fac0 2018-09-06 stsp free(commit);
324 a440fac0 2018-09-06 stsp return NULL;
325 a440fac0 2018-09-06 stsp }
326 a440fac0 2018-09-06 stsp
327 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
328 a440fac0 2018-09-06 stsp
329 a440fac0 2018-09-06 stsp return commit;
330 a440fac0 2018-09-06 stsp }
331 a440fac0 2018-09-06 stsp
332 a440fac0 2018-09-06 stsp const struct got_error *
333 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
334 a440fac0 2018-09-06 stsp const char *id_str)
335 a440fac0 2018-09-06 stsp {
336 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
337 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
338 a440fac0 2018-09-06 stsp
339 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
340 5df4932d 2018-11-05 stsp if (err)
341 7762fe12 2018-11-05 stsp return err;
342 a440fac0 2018-09-06 stsp
343 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&qid->id, id_str, GOT_HASH_SHA1)) {
344 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
346 a440fac0 2018-09-06 stsp return err;
347 a440fac0 2018-09-06 stsp }
348 a440fac0 2018-09-06 stsp
349 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
350 a440fac0 2018-09-06 stsp commit->nparents++;
351 a440fac0 2018-09-06 stsp
352 a440fac0 2018-09-06 stsp return NULL;
353 a440fac0 2018-09-06 stsp }
354 a440fac0 2018-09-06 stsp
355 a440fac0 2018-09-06 stsp static const struct got_error *
356 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
357 a440fac0 2018-09-06 stsp {
358 a440fac0 2018-09-06 stsp int sign = 1;
359 a440fac0 2018-09-06 stsp const char *p = tzstr;
360 a440fac0 2018-09-06 stsp time_t h, m;
361 a440fac0 2018-09-06 stsp
362 a440fac0 2018-09-06 stsp *gmtoff = 0;
363 a440fac0 2018-09-06 stsp
364 a440fac0 2018-09-06 stsp if (*p == '-')
365 a440fac0 2018-09-06 stsp sign = -1;
366 a440fac0 2018-09-06 stsp else if (*p != '+')
367 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
368 a440fac0 2018-09-06 stsp p++;
369 6771d425 2022-11-17 thomas if (!isdigit((unsigned char)*p) &&
370 6771d425 2022-11-17 thomas !isdigit((unsigned char)*(p + 1)))
371 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
372 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
373 a440fac0 2018-09-06 stsp
374 a440fac0 2018-09-06 stsp p += 2;
375 6771d425 2022-11-17 thomas if (!isdigit((unsigned char)*p) &&
376 6771d425 2022-11-17 thomas !isdigit((unsigned char)*(p + 1)))
377 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
378 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
379 a440fac0 2018-09-06 stsp
380 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
381 a440fac0 2018-09-06 stsp return NULL;
382 a440fac0 2018-09-06 stsp }
383 a440fac0 2018-09-06 stsp
384 a440fac0 2018-09-06 stsp static const struct got_error *
385 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
386 a440fac0 2018-09-06 stsp {
387 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
388 a440fac0 2018-09-06 stsp const char *errstr;
389 a440fac0 2018-09-06 stsp char *space, *tzstr;
390 a440fac0 2018-09-06 stsp
391 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
392 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
393 a440fac0 2018-09-06 stsp if (space == NULL)
394 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
395 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
396 a440fac0 2018-09-06 stsp if (tzstr == NULL)
397 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
398 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
399 a440fac0 2018-09-06 stsp free(tzstr);
400 b6b86fd1 2022-08-30 thomas if (err) {
401 9dbd8627 2021-02-04 stsp if (err->code != GOT_ERR_BAD_OBJ_DATA)
402 9dbd8627 2021-02-04 stsp return err;
403 9dbd8627 2021-02-04 stsp /* Old versions of Git omitted the timestamp. */
404 9dbd8627 2021-02-04 stsp *time = 0;
405 9dbd8627 2021-02-04 stsp *gmtoff = 0;
406 9dbd8627 2021-02-04 stsp return NULL;
407 9dbd8627 2021-02-04 stsp }
408 a440fac0 2018-09-06 stsp *space = '\0';
409 a440fac0 2018-09-06 stsp
410 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
411 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
412 a440fac0 2018-09-06 stsp if (space == NULL)
413 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
414 a440fac0 2018-09-06 stsp
415 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
416 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
417 a440fac0 2018-09-06 stsp if (errstr)
418 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
419 a440fac0 2018-09-06 stsp
420 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
421 a440fac0 2018-09-06 stsp *space = '\0';
422 a440fac0 2018-09-06 stsp
423 a440fac0 2018-09-06 stsp return NULL;
424 a440fac0 2018-09-06 stsp }
425 a440fac0 2018-09-06 stsp
426 03fa71c8 2018-09-06 stsp void
427 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
428 03fa71c8 2018-09-06 stsp {
429 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
430 03fa71c8 2018-09-06 stsp commit->refcnt--;
431 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
432 03fa71c8 2018-09-06 stsp return;
433 03fa71c8 2018-09-06 stsp }
434 03fa71c8 2018-09-06 stsp
435 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
436 03fa71c8 2018-09-06 stsp free(commit->tree_id);
437 03fa71c8 2018-09-06 stsp free(commit->author);
438 03fa71c8 2018-09-06 stsp free(commit->committer);
439 03fa71c8 2018-09-06 stsp free(commit->logmsg);
440 03fa71c8 2018-09-06 stsp free(commit);
441 45d799e2 2018-12-23 stsp }
442 45d799e2 2018-12-23 stsp
443 45d799e2 2018-12-23 stsp struct got_object_id *
444 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
445 45d799e2 2018-12-23 stsp {
446 45d799e2 2018-12-23 stsp return commit->tree_id;
447 45d799e2 2018-12-23 stsp }
448 45d799e2 2018-12-23 stsp
449 45d799e2 2018-12-23 stsp int
450 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
451 45d799e2 2018-12-23 stsp {
452 45d799e2 2018-12-23 stsp return commit->nparents;
453 03fa71c8 2018-09-06 stsp }
454 03fa71c8 2018-09-06 stsp
455 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
456 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
457 45d799e2 2018-12-23 stsp {
458 45d799e2 2018-12-23 stsp return &commit->parent_ids;
459 45d799e2 2018-12-23 stsp }
460 45d799e2 2018-12-23 stsp
461 45d799e2 2018-12-23 stsp const char *
462 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
463 45d799e2 2018-12-23 stsp {
464 45d799e2 2018-12-23 stsp return commit->author;
465 45d799e2 2018-12-23 stsp }
466 45d799e2 2018-12-23 stsp
467 45d799e2 2018-12-23 stsp time_t
468 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
469 45d799e2 2018-12-23 stsp {
470 45d799e2 2018-12-23 stsp return commit->author_time;
471 45d799e2 2018-12-23 stsp }
472 45d799e2 2018-12-23 stsp
473 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
474 45d799e2 2018-12-23 stsp {
475 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
476 45d799e2 2018-12-23 stsp }
477 45d799e2 2018-12-23 stsp
478 45d799e2 2018-12-23 stsp const char *
479 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
480 45d799e2 2018-12-23 stsp {
481 45d799e2 2018-12-23 stsp return commit->committer;
482 45d799e2 2018-12-23 stsp }
483 45d799e2 2018-12-23 stsp
484 45d799e2 2018-12-23 stsp time_t
485 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
486 45d799e2 2018-12-23 stsp {
487 45d799e2 2018-12-23 stsp return commit->committer_time;
488 45d799e2 2018-12-23 stsp }
489 45d799e2 2018-12-23 stsp
490 45d799e2 2018-12-23 stsp time_t
491 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
492 45d799e2 2018-12-23 stsp {
493 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
494 45d799e2 2018-12-23 stsp }
495 5943eee2 2019-08-13 stsp
496 5943eee2 2019-08-13 stsp const struct got_error *
497 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
498 45d799e2 2018-12-23 stsp {
499 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
500 b9c41b54 2021-08-03 stsp const char *src;
501 b9c41b54 2021-08-03 stsp char *dst;
502 5943eee2 2019-08-13 stsp size_t len;
503 5943eee2 2019-08-13 stsp
504 b9c41b54 2021-08-03 stsp len = strlen(commit->logmsg);
505 b9c41b54 2021-08-03 stsp *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
506 b9c41b54 2021-08-03 stsp if (*logmsg == NULL)
507 b9c41b54 2021-08-03 stsp return got_error_from_errno("malloc");
508 5943eee2 2019-08-13 stsp
509 b9c41b54 2021-08-03 stsp /*
510 b9c41b54 2021-08-03 stsp * Strip out unusual headers. Headers are separated from the commit
511 b9c41b54 2021-08-03 stsp * message body by a single empty line.
512 b9c41b54 2021-08-03 stsp */
513 b9c41b54 2021-08-03 stsp src = commit->logmsg;
514 b9c41b54 2021-08-03 stsp dst = *logmsg;
515 b9c41b54 2021-08-03 stsp while (*src != '\0' && *src != '\n') {
516 b9c41b54 2021-08-03 stsp int copy_header = 1, eol = 0;
517 b9c41b54 2021-08-03 stsp if (strncmp(src, GOT_COMMIT_LABEL_TREE,
518 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
519 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
520 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
521 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_PARENT,
522 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
523 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
524 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
525 b9c41b54 2021-08-03 stsp copy_header = 0;
526 13555e04 2019-09-28 semarie
527 b9c41b54 2021-08-03 stsp while (*src != '\0' && !eol) {
528 b9c41b54 2021-08-03 stsp if (copy_header) {
529 b9c41b54 2021-08-03 stsp *dst = *src;
530 b9c41b54 2021-08-03 stsp dst++;
531 b9c41b54 2021-08-03 stsp }
532 b9c41b54 2021-08-03 stsp if (*src == '\n')
533 b9c41b54 2021-08-03 stsp eol = 1;
534 b9c41b54 2021-08-03 stsp src++;
535 5943eee2 2019-08-13 stsp }
536 b9c41b54 2021-08-03 stsp }
537 b9c41b54 2021-08-03 stsp *dst = '\0';
538 13555e04 2019-09-28 semarie
539 b9c41b54 2021-08-03 stsp if (strlcat(*logmsg, src, len + 1) >= len + 1) {
540 b9c41b54 2021-08-03 stsp err = got_error(GOT_ERR_NO_SPACE);
541 b9c41b54 2021-08-03 stsp goto done;
542 ef744db3 2020-08-27 stsp }
543 ef744db3 2020-08-27 stsp
544 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
545 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
546 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
547 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
548 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
549 5943eee2 2019-08-13 stsp len--;
550 5943eee2 2019-08-13 stsp }
551 b9c41b54 2021-08-03 stsp
552 b9c41b54 2021-08-03 stsp /* Append a trailing newline if missing. */
553 b9c41b54 2021-08-03 stsp if (len > 0 && (*logmsg)[len - 1] != '\n') {
554 b9c41b54 2021-08-03 stsp (*logmsg)[len] = '\n';
555 b9c41b54 2021-08-03 stsp (*logmsg)[len + 1] = '\0';
556 b9c41b54 2021-08-03 stsp }
557 5943eee2 2019-08-13 stsp done:
558 5943eee2 2019-08-13 stsp if (err) {
559 5943eee2 2019-08-13 stsp free(*logmsg);
560 5943eee2 2019-08-13 stsp *logmsg = NULL;
561 5943eee2 2019-08-13 stsp }
562 5943eee2 2019-08-13 stsp return err;
563 24ea5512 2019-08-22 stsp }
564 24ea5512 2019-08-22 stsp
565 24ea5512 2019-08-22 stsp const char *
566 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
567 24ea5512 2019-08-22 stsp {
568 24ea5512 2019-08-22 stsp return commit->logmsg;
569 45d799e2 2018-12-23 stsp }
570 45d799e2 2018-12-23 stsp
571 a440fac0 2018-09-06 stsp const struct got_error *
572 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
573 5e0b25c4 2018-12-24 stsp size_t len)
574 a440fac0 2018-09-06 stsp {
575 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
576 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo = GOT_HASH_SHA1;
577 a440fac0 2018-09-06 stsp char *s = buf;
578 ff2a4428 2019-03-19 stsp size_t label_len;
579 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
580 4793d91b 2019-09-22 stsp
581 4793d91b 2019-09-22 stsp if (remain == 0)
582 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
583 230a42bd 2019-05-11 jcs
584 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
585 a440fac0 2018-09-06 stsp if (*commit == NULL)
586 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
587 a440fac0 2018-09-06 stsp
588 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
589 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
590 ff2a4428 2019-03-19 stsp remain -= label_len;
591 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
592 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
593 a440fac0 2018-09-06 stsp goto done;
594 a440fac0 2018-09-06 stsp }
595 ff2a4428 2019-03-19 stsp s += label_len;
596 c8ae092d 2023-02-23 thomas if (!got_parse_object_id((*commit)->tree_id, s, algo)) {
597 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
598 a440fac0 2018-09-06 stsp goto done;
599 a440fac0 2018-09-06 stsp }
600 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
601 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
602 a440fac0 2018-09-06 stsp } else {
603 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
604 a440fac0 2018-09-06 stsp goto done;
605 a440fac0 2018-09-06 stsp }
606 a440fac0 2018-09-06 stsp
607 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
608 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
609 ff2a4428 2019-03-19 stsp remain -= label_len;
610 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
611 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
612 a440fac0 2018-09-06 stsp goto done;
613 a440fac0 2018-09-06 stsp }
614 ff2a4428 2019-03-19 stsp s += label_len;
615 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
616 a440fac0 2018-09-06 stsp if (err)
617 a440fac0 2018-09-06 stsp goto done;
618 a440fac0 2018-09-06 stsp
619 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
620 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
621 a440fac0 2018-09-06 stsp }
622 a440fac0 2018-09-06 stsp
623 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
624 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
625 a440fac0 2018-09-06 stsp char *p;
626 a440fac0 2018-09-06 stsp size_t slen;
627 a440fac0 2018-09-06 stsp
628 ff2a4428 2019-03-19 stsp remain -= label_len;
629 a440fac0 2018-09-06 stsp if (remain <= 0) {
630 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
631 a440fac0 2018-09-06 stsp goto done;
632 a440fac0 2018-09-06 stsp }
633 ff2a4428 2019-03-19 stsp s += label_len;
634 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
635 a440fac0 2018-09-06 stsp if (p == NULL) {
636 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 a440fac0 2018-09-06 stsp goto done;
638 a440fac0 2018-09-06 stsp }
639 a440fac0 2018-09-06 stsp *p = '\0';
640 a440fac0 2018-09-06 stsp slen = strlen(s);
641 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
642 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
643 a440fac0 2018-09-06 stsp if (err)
644 a440fac0 2018-09-06 stsp goto done;
645 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
646 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
647 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
648 a440fac0 2018-09-06 stsp goto done;
649 a440fac0 2018-09-06 stsp }
650 a440fac0 2018-09-06 stsp s += slen + 1;
651 a440fac0 2018-09-06 stsp remain -= slen + 1;
652 a440fac0 2018-09-06 stsp }
653 a440fac0 2018-09-06 stsp
654 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
655 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
656 a440fac0 2018-09-06 stsp char *p;
657 a440fac0 2018-09-06 stsp size_t slen;
658 a440fac0 2018-09-06 stsp
659 ff2a4428 2019-03-19 stsp remain -= label_len;
660 a440fac0 2018-09-06 stsp if (remain <= 0) {
661 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
662 a440fac0 2018-09-06 stsp goto done;
663 a440fac0 2018-09-06 stsp }
664 ff2a4428 2019-03-19 stsp s += label_len;
665 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
666 a440fac0 2018-09-06 stsp if (p == NULL) {
667 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
668 a440fac0 2018-09-06 stsp goto done;
669 a440fac0 2018-09-06 stsp }
670 a440fac0 2018-09-06 stsp *p = '\0';
671 a440fac0 2018-09-06 stsp slen = strlen(s);
672 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
673 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
674 a440fac0 2018-09-06 stsp if (err)
675 a440fac0 2018-09-06 stsp goto done;
676 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
677 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
678 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
679 a440fac0 2018-09-06 stsp goto done;
680 a440fac0 2018-09-06 stsp }
681 a440fac0 2018-09-06 stsp s += slen + 1;
682 a440fac0 2018-09-06 stsp remain -= slen + 1;
683 a440fac0 2018-09-06 stsp }
684 a440fac0 2018-09-06 stsp
685 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
686 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
687 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
688 a440fac0 2018-09-06 stsp goto done;
689 a440fac0 2018-09-06 stsp }
690 a440fac0 2018-09-06 stsp done:
691 a440fac0 2018-09-06 stsp if (err) {
692 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
693 a440fac0 2018-09-06 stsp *commit = NULL;
694 a440fac0 2018-09-06 stsp }
695 a440fac0 2018-09-06 stsp return err;
696 ed175427 2019-05-09 stsp }
697 3efd8e31 2022-10-23 thomas
698 3efd8e31 2022-10-23 thomas const struct got_error *
699 3efd8e31 2022-10-23 thomas got_object_read_commit(struct got_commit_object **commit, int fd,
700 3efd8e31 2022-10-23 thomas struct got_object_id *expected_id, size_t expected_size)
701 3efd8e31 2022-10-23 thomas {
702 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
703 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
704 3efd8e31 2022-10-23 thomas size_t len;
705 3efd8e31 2022-10-23 thomas uint8_t *p;
706 3efd8e31 2022-10-23 thomas struct got_inflate_checksum csum;
707 b16893ba 2023-02-24 thomas struct got_hash ctx;
708 3efd8e31 2022-10-23 thomas struct got_object_id id;
709 ed175427 2019-05-09 stsp
710 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
711 3efd8e31 2022-10-23 thomas memset(&csum, 0, sizeof(csum));
712 b16893ba 2023-02-24 thomas csum.output_ctx = &ctx;
713 3efd8e31 2022-10-23 thomas
714 3efd8e31 2022-10-23 thomas err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
715 3efd8e31 2022-10-23 thomas if (err)
716 3efd8e31 2022-10-23 thomas return err;
717 3efd8e31 2022-10-23 thomas
718 b16893ba 2023-02-24 thomas got_hash_final_object_id(&ctx, &id);
719 1ba62ba4 2023-02-03 thomas if (got_object_id_cmp(expected_id, &id) != 0) {
720 dc43cdc9 2023-02-07 thomas err = got_error_checksum(expected_id);
721 3efd8e31 2022-10-23 thomas goto done;
722 3efd8e31 2022-10-23 thomas }
723 3efd8e31 2022-10-23 thomas
724 3efd8e31 2022-10-23 thomas err = got_object_parse_header(&obj, p, len);
725 3efd8e31 2022-10-23 thomas if (err)
726 3efd8e31 2022-10-23 thomas goto done;
727 3efd8e31 2022-10-23 thomas
728 3efd8e31 2022-10-23 thomas if (len < obj->hdrlen + obj->size) {
729 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_OBJ_DATA);
730 3efd8e31 2022-10-23 thomas goto done;
731 3efd8e31 2022-10-23 thomas }
732 3efd8e31 2022-10-23 thomas
733 3efd8e31 2022-10-23 thomas if (obj->type != GOT_OBJ_TYPE_COMMIT) {
734 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
735 3efd8e31 2022-10-23 thomas goto done;
736 3efd8e31 2022-10-23 thomas }
737 3efd8e31 2022-10-23 thomas
738 3efd8e31 2022-10-23 thomas /* Skip object header. */
739 3efd8e31 2022-10-23 thomas len -= obj->hdrlen;
740 3efd8e31 2022-10-23 thomas err = got_object_parse_commit(commit, p + obj->hdrlen, len);
741 3efd8e31 2022-10-23 thomas done:
742 3efd8e31 2022-10-23 thomas free(p);
743 3efd8e31 2022-10-23 thomas if (obj)
744 3efd8e31 2022-10-23 thomas got_object_close(obj);
745 3efd8e31 2022-10-23 thomas return err;
746 3efd8e31 2022-10-23 thomas }
747 3efd8e31 2022-10-23 thomas
748 ed175427 2019-05-09 stsp void
749 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
750 ed175427 2019-05-09 stsp {
751 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
752 03fa71c8 2018-09-06 stsp tree->refcnt--;
753 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
754 03fa71c8 2018-09-06 stsp return;
755 03fa71c8 2018-09-06 stsp }
756 03fa71c8 2018-09-06 stsp
757 56e0773d 2019-11-28 stsp free(tree->entries);
758 03fa71c8 2018-09-06 stsp free(tree);
759 03fa71c8 2018-09-06 stsp }
760 03fa71c8 2018-09-06 stsp
761 2d28509d 2023-04-28 thomas const struct got_error *
762 2d28509d 2023-04-28 thomas got_object_parse_tree_entry(struct got_parsed_tree_entry *pte, size_t *elen,
763 2d28509d 2023-04-28 thomas char *buf, size_t maxlen)
764 a440fac0 2018-09-06 stsp {
765 8914529d 2019-04-13 stsp char *p, *space;
766 a0de39f3 2019-08-09 stsp
767 a0de39f3 2019-08-09 stsp *elen = 0;
768 a440fac0 2018-09-06 stsp
769 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
770 78e7b7b8 2022-05-19 thomas if (*elen > maxlen)
771 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
772 a440fac0 2018-09-06 stsp
773 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
774 78e7b7b8 2022-05-19 thomas if (space == NULL || space <= buf)
775 78e7b7b8 2022-05-19 thomas return got_error(GOT_ERR_BAD_OBJ_DATA);
776 78e7b7b8 2022-05-19 thomas
777 78e7b7b8 2022-05-19 thomas pte->mode = 0;
778 8914529d 2019-04-13 stsp p = buf;
779 8914529d 2019-04-13 stsp while (p < space) {
780 a8771ebd 2023-01-19 thomas if (*p < '0' || *p > '7')
781 78e7b7b8 2022-05-19 thomas return got_error(GOT_ERR_BAD_OBJ_DATA);
782 78e7b7b8 2022-05-19 thomas pte->mode <<= 3;
783 78e7b7b8 2022-05-19 thomas pte->mode |= *p - '0';
784 a440fac0 2018-09-06 stsp p++;
785 a440fac0 2018-09-06 stsp }
786 a440fac0 2018-09-06 stsp
787 78e7b7b8 2022-05-19 thomas if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH)
788 78e7b7b8 2022-05-19 thomas return got_error(GOT_ERR_BAD_OBJ_DATA);
789 78e7b7b8 2022-05-19 thomas
790 78e7b7b8 2022-05-19 thomas pte->name = space + 1;
791 78e7b7b8 2022-05-19 thomas pte->namelen = strlen(pte->name);
792 68bf1b1e 2018-11-07 stsp buf += *elen;
793 78e7b7b8 2022-05-19 thomas pte->id = buf;
794 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
795 78e7b7b8 2022-05-19 thomas return NULL;
796 a440fac0 2018-09-06 stsp }
797 a440fac0 2018-09-06 stsp
798 78e7b7b8 2022-05-19 thomas static int
799 78e7b7b8 2022-05-19 thomas pte_cmp(const void *pa, const void *pb)
800 78e7b7b8 2022-05-19 thomas {
801 78e7b7b8 2022-05-19 thomas const struct got_parsed_tree_entry *a = pa, *b = pb;
802 78e7b7b8 2022-05-19 thomas
803 78e7b7b8 2022-05-19 thomas return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
804 78e7b7b8 2022-05-19 thomas }
805 78e7b7b8 2022-05-19 thomas
806 a440fac0 2018-09-06 stsp const struct got_error *
807 c77e00b3 2022-10-18 thomas got_object_parse_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
808 c77e00b3 2022-10-18 thomas size_t *nentries_alloc, uint8_t *buf, size_t len)
809 a440fac0 2018-09-06 stsp {
810 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
811 c77e00b3 2022-10-18 thomas size_t remain = len;
812 78e7b7b8 2022-05-19 thomas const size_t nalloc = 16;
813 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *pte;
814 78e7b7b8 2022-05-19 thomas int i;
815 f5d3d7af 2019-02-05 stsp
816 3022d272 2019-11-14 stsp *nentries = 0;
817 db1d3576 2019-10-04 stsp if (remain == 0)
818 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
819 db1d3576 2019-10-04 stsp
820 a440fac0 2018-09-06 stsp while (remain > 0) {
821 a440fac0 2018-09-06 stsp size_t elen;
822 a440fac0 2018-09-06 stsp
823 c77e00b3 2022-10-18 thomas if (*nentries >= *nentries_alloc) {
824 c77e00b3 2022-10-18 thomas pte = recallocarray(*entries, *nentries_alloc,
825 c77e00b3 2022-10-18 thomas *nentries_alloc + nalloc, sizeof(**entries));
826 78e7b7b8 2022-05-19 thomas if (pte == NULL) {
827 78e7b7b8 2022-05-19 thomas err = got_error_from_errno("recallocarray");
828 78e7b7b8 2022-05-19 thomas goto done;
829 78e7b7b8 2022-05-19 thomas }
830 78e7b7b8 2022-05-19 thomas *entries = pte;
831 c77e00b3 2022-10-18 thomas *nentries_alloc += nalloc;
832 78e7b7b8 2022-05-19 thomas }
833 78e7b7b8 2022-05-19 thomas
834 78e7b7b8 2022-05-19 thomas pte = &(*entries)[*nentries];
835 2d28509d 2023-04-28 thomas err = got_object_parse_tree_entry(pte, &elen, buf, remain);
836 a440fac0 2018-09-06 stsp if (err)
837 f5d3d7af 2019-02-05 stsp goto done;
838 a440fac0 2018-09-06 stsp buf += elen;
839 a440fac0 2018-09-06 stsp remain -= elen;
840 3022d272 2019-11-14 stsp (*nentries)++;
841 a440fac0 2018-09-06 stsp }
842 a440fac0 2018-09-06 stsp
843 a440fac0 2018-09-06 stsp if (remain != 0) {
844 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
845 f5d3d7af 2019-02-05 stsp goto done;
846 a440fac0 2018-09-06 stsp }
847 78e7b7b8 2022-05-19 thomas
848 78e7b7b8 2022-05-19 thomas if (*nentries > 1) {
849 78e7b7b8 2022-05-19 thomas mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
850 78e7b7b8 2022-05-19 thomas
851 78e7b7b8 2022-05-19 thomas for (i = 0; i < *nentries - 1; i++) {
852 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *prev = &(*entries)[i];
853 78e7b7b8 2022-05-19 thomas pte = &(*entries)[i + 1];
854 78e7b7b8 2022-05-19 thomas if (got_path_cmp(prev->name, pte->name,
855 78e7b7b8 2022-05-19 thomas prev->namelen, pte->namelen) == 0) {
856 78e7b7b8 2022-05-19 thomas err = got_error(GOT_ERR_TREE_DUP_ENTRY);
857 78e7b7b8 2022-05-19 thomas break;
858 78e7b7b8 2022-05-19 thomas }
859 78e7b7b8 2022-05-19 thomas }
860 78e7b7b8 2022-05-19 thomas }
861 3022d272 2019-11-14 stsp done:
862 c77e00b3 2022-10-18 thomas if (err)
863 3022d272 2019-11-14 stsp *nentries = 0;
864 f5d3d7af 2019-02-05 stsp return err;
865 b64b1f95 2020-01-06 stsp }
866 b64b1f95 2020-01-06 stsp
867 3efd8e31 2022-10-23 thomas const struct got_error *
868 3efd8e31 2022-10-23 thomas got_object_read_tree(struct got_parsed_tree_entry **entries, size_t *nentries,
869 3efd8e31 2022-10-23 thomas size_t *nentries_alloc, uint8_t **p, int fd,
870 3efd8e31 2022-10-23 thomas struct got_object_id *expected_id)
871 3efd8e31 2022-10-23 thomas {
872 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
873 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
874 3efd8e31 2022-10-23 thomas size_t len;
875 3efd8e31 2022-10-23 thomas struct got_inflate_checksum csum;
876 b16893ba 2023-02-24 thomas struct got_hash ctx;
877 3efd8e31 2022-10-23 thomas struct got_object_id id;
878 3efd8e31 2022-10-23 thomas
879 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
880 3efd8e31 2022-10-23 thomas memset(&csum, 0, sizeof(csum));
881 b16893ba 2023-02-24 thomas csum.output_ctx = &ctx;
882 3efd8e31 2022-10-23 thomas
883 3efd8e31 2022-10-23 thomas err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
884 3efd8e31 2022-10-23 thomas if (err)
885 3efd8e31 2022-10-23 thomas return err;
886 3efd8e31 2022-10-23 thomas
887 b16893ba 2023-02-24 thomas got_hash_final_object_id(&ctx, &id);
888 1ba62ba4 2023-02-03 thomas if (got_object_id_cmp(expected_id, &id) != 0) {
889 dc43cdc9 2023-02-07 thomas err = got_error_checksum(expected_id);
890 3efd8e31 2022-10-23 thomas goto done;
891 3efd8e31 2022-10-23 thomas }
892 3efd8e31 2022-10-23 thomas
893 3efd8e31 2022-10-23 thomas err = got_object_parse_header(&obj, *p, len);
894 3efd8e31 2022-10-23 thomas if (err)
895 3efd8e31 2022-10-23 thomas goto done;
896 3efd8e31 2022-10-23 thomas
897 3efd8e31 2022-10-23 thomas if (len < obj->hdrlen + obj->size) {
898 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_OBJ_DATA);
899 3efd8e31 2022-10-23 thomas goto done;
900 3efd8e31 2022-10-23 thomas }
901 3efd8e31 2022-10-23 thomas
902 3efd8e31 2022-10-23 thomas /* Skip object header. */
903 3efd8e31 2022-10-23 thomas len -= obj->hdrlen;
904 3efd8e31 2022-10-23 thomas err = got_object_parse_tree(entries, nentries, nentries_alloc,
905 3efd8e31 2022-10-23 thomas *p + obj->hdrlen, len);
906 3efd8e31 2022-10-23 thomas done:
907 3efd8e31 2022-10-23 thomas if (obj)
908 3efd8e31 2022-10-23 thomas got_object_close(obj);
909 3efd8e31 2022-10-23 thomas return err;
910 3efd8e31 2022-10-23 thomas }
911 3efd8e31 2022-10-23 thomas
912 b64b1f95 2020-01-06 stsp void
913 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
914 f4a881ce 2018-11-17 stsp {
915 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
916 ca0d469c 2019-08-13 stsp tag->refcnt--;
917 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
918 ca0d469c 2019-08-13 stsp return;
919 ca0d469c 2019-08-13 stsp }
920 ca0d469c 2019-08-13 stsp
921 f4a881ce 2018-11-17 stsp free(tag->tag);
922 f4a881ce 2018-11-17 stsp free(tag->tagger);
923 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
924 f4a881ce 2018-11-17 stsp free(tag);
925 f4a881ce 2018-11-17 stsp }
926 f4a881ce 2018-11-17 stsp
927 ad242220 2018-09-08 stsp const struct got_error *
928 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
929 f4a881ce 2018-11-17 stsp {
930 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
931 c8ae092d 2023-02-23 thomas enum got_hash_algorithm algo = GOT_HASH_SHA1;
932 f4a881ce 2018-11-17 stsp size_t remain = len;
933 f4a881ce 2018-11-17 stsp char *s = buf;
934 ff2a4428 2019-03-19 stsp size_t label_len;
935 4793d91b 2019-09-22 stsp
936 4793d91b 2019-09-22 stsp if (remain == 0)
937 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
938 f4a881ce 2018-11-17 stsp
939 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
940 f4a881ce 2018-11-17 stsp if (*tag == NULL)
941 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
942 f4a881ce 2018-11-17 stsp
943 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
944 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
945 ff2a4428 2019-03-19 stsp remain -= label_len;
946 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
947 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
948 f4a881ce 2018-11-17 stsp goto done;
949 f4a881ce 2018-11-17 stsp }
950 ff2a4428 2019-03-19 stsp s += label_len;
951 c8ae092d 2023-02-23 thomas if (!got_parse_object_id(&(*tag)->id, s, algo)) {
952 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
953 f4a881ce 2018-11-17 stsp goto done;
954 f4a881ce 2018-11-17 stsp }
955 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
956 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
957 f4a881ce 2018-11-17 stsp } else {
958 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
959 f4a881ce 2018-11-17 stsp goto done;
960 f4a881ce 2018-11-17 stsp }
961 f4a881ce 2018-11-17 stsp
962 f4a881ce 2018-11-17 stsp if (remain <= 0) {
963 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
964 f4a881ce 2018-11-17 stsp goto done;
965 f4a881ce 2018-11-17 stsp }
966 f4a881ce 2018-11-17 stsp
967 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
968 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
969 ff2a4428 2019-03-19 stsp remain -= label_len;
970 f4a881ce 2018-11-17 stsp if (remain <= 0) {
971 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
972 f4a881ce 2018-11-17 stsp goto done;
973 f4a881ce 2018-11-17 stsp }
974 ff2a4428 2019-03-19 stsp s += label_len;
975 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
976 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
977 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
978 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
979 ff2a4428 2019-03-19 stsp s += label_len;
980 ff2a4428 2019-03-19 stsp remain -= label_len;
981 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
982 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
983 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
984 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
985 ff2a4428 2019-03-19 stsp s += label_len;
986 ff2a4428 2019-03-19 stsp remain -= label_len;
987 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
988 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
989 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
990 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
991 ff2a4428 2019-03-19 stsp s += label_len;
992 ff2a4428 2019-03-19 stsp remain -= label_len;
993 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
994 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
995 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
996 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
997 ff2a4428 2019-03-19 stsp s += label_len;
998 ff2a4428 2019-03-19 stsp remain -= label_len;
999 f4a881ce 2018-11-17 stsp } else {
1000 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1001 f4a881ce 2018-11-17 stsp goto done;
1002 f4a881ce 2018-11-17 stsp }
1003 f4a881ce 2018-11-17 stsp
1004 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
1005 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1006 f4a881ce 2018-11-17 stsp goto done;
1007 f4a881ce 2018-11-17 stsp }
1008 f4a881ce 2018-11-17 stsp s++;
1009 f4a881ce 2018-11-17 stsp remain--;
1010 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1011 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1012 f4a881ce 2018-11-17 stsp goto done;
1013 f4a881ce 2018-11-17 stsp }
1014 f4a881ce 2018-11-17 stsp } else {
1015 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1016 f4a881ce 2018-11-17 stsp goto done;
1017 f4a881ce 2018-11-17 stsp }
1018 f4a881ce 2018-11-17 stsp
1019 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
1020 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
1021 f4a881ce 2018-11-17 stsp char *p;
1022 f4a881ce 2018-11-17 stsp size_t slen;
1023 ff2a4428 2019-03-19 stsp remain -= label_len;
1024 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1025 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1026 f4a881ce 2018-11-17 stsp goto done;
1027 f4a881ce 2018-11-17 stsp }
1028 ff2a4428 2019-03-19 stsp s += label_len;
1029 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1030 f4a881ce 2018-11-17 stsp if (p == NULL) {
1031 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1032 f4a881ce 2018-11-17 stsp goto done;
1033 f4a881ce 2018-11-17 stsp }
1034 f4a881ce 2018-11-17 stsp *p = '\0';
1035 f4a881ce 2018-11-17 stsp slen = strlen(s);
1036 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
1037 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
1038 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1039 f4a881ce 2018-11-17 stsp goto done;
1040 f4a881ce 2018-11-17 stsp }
1041 f4a881ce 2018-11-17 stsp s += slen + 1;
1042 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1043 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1044 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1045 f4a881ce 2018-11-17 stsp goto done;
1046 f4a881ce 2018-11-17 stsp }
1047 f4a881ce 2018-11-17 stsp } else {
1048 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 f4a881ce 2018-11-17 stsp goto done;
1050 f4a881ce 2018-11-17 stsp }
1051 f4a881ce 2018-11-17 stsp
1052 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
1053 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
1054 f4a881ce 2018-11-17 stsp char *p;
1055 f4a881ce 2018-11-17 stsp size_t slen;
1056 f4a881ce 2018-11-17 stsp
1057 ff2a4428 2019-03-19 stsp remain -= label_len;
1058 f4a881ce 2018-11-17 stsp if (remain <= 0) {
1059 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1060 f4a881ce 2018-11-17 stsp goto done;
1061 f4a881ce 2018-11-17 stsp }
1062 ff2a4428 2019-03-19 stsp s += label_len;
1063 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
1064 f4a881ce 2018-11-17 stsp if (p == NULL) {
1065 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1066 f4a881ce 2018-11-17 stsp goto done;
1067 f4a881ce 2018-11-17 stsp }
1068 f4a881ce 2018-11-17 stsp *p = '\0';
1069 f4a881ce 2018-11-17 stsp slen = strlen(s);
1070 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
1071 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
1072 f4a881ce 2018-11-17 stsp if (err)
1073 f4a881ce 2018-11-17 stsp goto done;
1074 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
1075 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
1076 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1077 f4a881ce 2018-11-17 stsp goto done;
1078 f4a881ce 2018-11-17 stsp }
1079 f4a881ce 2018-11-17 stsp s += slen + 1;
1080 f4a881ce 2018-11-17 stsp remain -= slen + 1;
1081 5a8b373c 2020-12-18 stsp if (remain < 0) {
1082 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1083 f4a881ce 2018-11-17 stsp goto done;
1084 f4a881ce 2018-11-17 stsp }
1085 f4a881ce 2018-11-17 stsp } else {
1086 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
1087 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
1088 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
1089 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1090 e0e55b50 2019-02-01 stsp goto done;
1091 e0e55b50 2019-02-01 stsp }
1092 f4a881ce 2018-11-17 stsp }
1093 f4a881ce 2018-11-17 stsp
1094 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
1095 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
1096 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
1097 f4a881ce 2018-11-17 stsp goto done;
1098 f4a881ce 2018-11-17 stsp }
1099 f4a881ce 2018-11-17 stsp done:
1100 f4a881ce 2018-11-17 stsp if (err) {
1101 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
1102 f4a881ce 2018-11-17 stsp *tag = NULL;
1103 3efd8e31 2022-10-23 thomas }
1104 3efd8e31 2022-10-23 thomas return err;
1105 3efd8e31 2022-10-23 thomas }
1106 3efd8e31 2022-10-23 thomas
1107 3efd8e31 2022-10-23 thomas const struct got_error *
1108 946e0798 2022-11-06 thomas got_object_read_tag(struct got_tag_object **tag, int fd,
1109 3efd8e31 2022-10-23 thomas struct got_object_id *expected_id, size_t expected_size)
1110 3efd8e31 2022-10-23 thomas {
1111 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
1112 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
1113 3efd8e31 2022-10-23 thomas size_t len;
1114 3efd8e31 2022-10-23 thomas uint8_t *p;
1115 3efd8e31 2022-10-23 thomas struct got_inflate_checksum csum;
1116 b16893ba 2023-02-24 thomas struct got_hash ctx;
1117 3efd8e31 2022-10-23 thomas struct got_object_id id;
1118 3efd8e31 2022-10-23 thomas
1119 b16893ba 2023-02-24 thomas got_hash_init(&ctx, GOT_HASH_SHA1);
1120 3efd8e31 2022-10-23 thomas memset(&csum, 0, sizeof(csum));
1121 b16893ba 2023-02-24 thomas csum.output_ctx = &ctx;
1122 3efd8e31 2022-10-23 thomas
1123 3efd8e31 2022-10-23 thomas err = got_inflate_to_mem_fd(&p, &len, NULL, &csum,
1124 3efd8e31 2022-10-23 thomas expected_size, fd);
1125 3efd8e31 2022-10-23 thomas if (err)
1126 3efd8e31 2022-10-23 thomas return err;
1127 3efd8e31 2022-10-23 thomas
1128 b16893ba 2023-02-24 thomas got_hash_final_object_id(&ctx, &id);
1129 1ba62ba4 2023-02-03 thomas if (got_object_id_cmp(expected_id, &id) != 0) {
1130 dc43cdc9 2023-02-07 thomas err = got_error_checksum(expected_id);
1131 3efd8e31 2022-10-23 thomas goto done;
1132 f4a881ce 2018-11-17 stsp }
1133 3efd8e31 2022-10-23 thomas
1134 3efd8e31 2022-10-23 thomas err = got_object_parse_header(&obj, p, len);
1135 3efd8e31 2022-10-23 thomas if (err)
1136 3efd8e31 2022-10-23 thomas goto done;
1137 3efd8e31 2022-10-23 thomas
1138 3efd8e31 2022-10-23 thomas if (len < obj->hdrlen + obj->size) {
1139 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_BAD_OBJ_DATA);
1140 3efd8e31 2022-10-23 thomas goto done;
1141 3efd8e31 2022-10-23 thomas }
1142 3efd8e31 2022-10-23 thomas
1143 3efd8e31 2022-10-23 thomas /* Skip object header. */
1144 3efd8e31 2022-10-23 thomas len -= obj->hdrlen;
1145 3efd8e31 2022-10-23 thomas err = got_object_parse_tag(tag, p + obj->hdrlen, len);
1146 3efd8e31 2022-10-23 thomas done:
1147 3efd8e31 2022-10-23 thomas free(p);
1148 3efd8e31 2022-10-23 thomas if (obj)
1149 3efd8e31 2022-10-23 thomas got_object_close(obj);
1150 f4a881ce 2018-11-17 stsp return err;
1151 f4a881ce 2018-11-17 stsp }