2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
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>
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>
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"
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"
54 a440fac0 2018-09-06 stsp #ifndef nitems
55 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
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)
61 6d7eb4f7 2023-04-04 thomas const struct got_error *err = NULL;
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;
67 6d7eb4f7 2023-04-04 thomas case GOT_OBJ_TYPE_TREE:
68 6d7eb4f7 2023-04-04 thomas *label = GOT_OBJ_LABEL_TREE;
70 6d7eb4f7 2023-04-04 thomas case GOT_OBJ_TYPE_COMMIT:
71 6d7eb4f7 2023-04-04 thomas *label = GOT_OBJ_LABEL_COMMIT;
73 6d7eb4f7 2023-04-04 thomas case GOT_OBJ_TYPE_TAG:
74 6d7eb4f7 2023-04-04 thomas *label = GOT_OBJ_LABEL_TAG;
77 6d7eb4f7 2023-04-04 thomas *label = NULL;
78 6d7eb4f7 2023-04-04 thomas err = got_error(GOT_ERR_OBJ_TYPE);
82 6d7eb4f7 2023-04-04 thomas return err;
86 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
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)
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);
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)
108 8ab9215c 2021-10-15 thomas const struct got_error *err = NULL;
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;
116 3efd8e31 2022-10-23 thomas if (obj->close_cb)
117 3efd8e31 2022-10-23 thomas obj->close_cb(obj);
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");
126 2b0ae357 2022-01-10 thomas free(obj->data);
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");
131 8ab9215c 2021-10-15 thomas free(obj);
132 8ab9215c 2021-10-15 thomas return err;
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)
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,
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,
150 1785f84a 2018-12-23 stsp int type = 0;
151 9b31ed65 2022-02-12 thomas size_t size = 0;
153 9b31ed65 2022-02-12 thomas char *end;
155 1785f84a 2018-12-23 stsp *obj = NULL;
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);
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;
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)
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);
177 1785f84a 2018-12-23 stsp if (type == 0)
178 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
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;
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)
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;
199 1785f84a 2018-12-23 stsp *obj = NULL;
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';
206 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, buf, zbsize, NULL);
208 1785f84a 2018-12-23 stsp return err;
210 1785f84a 2018-12-23 stsp totlen = 0;
212 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
215 1785f84a 2018-12-23 stsp if (outlen == 0)
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;
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");
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;
230 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
232 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
235 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
236 1785f84a 2018-12-23 stsp return err;
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)
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;
252 3efd8e31 2022-10-23 thomas *outbuf = NULL;
253 3efd8e31 2022-10-23 thomas *size = 0;
254 3efd8e31 2022-10-23 thomas *hdrlen = 0;
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;
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");
263 3efd8e31 2022-10-23 thomas err = got_object_read_header(&obj, infd);
265 3efd8e31 2022-10-23 thomas return err;
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");
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);
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().
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;
288 3efd8e31 2022-10-23 thomas err = got_inflate_to_fd(&len, f, &csum, outfd);
291 3efd8e31 2022-10-23 thomas goto done;
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;
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;
304 3efd8e31 2022-10-23 thomas *size = obj->size;
305 3efd8e31 2022-10-23 thomas *hdrlen = obj->hdrlen;
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;
313 a440fac0 2018-09-06 stsp struct got_commit_object *
314 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
316 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
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;
327 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
329 a440fac0 2018-09-06 stsp return commit;
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)
336 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
337 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
339 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
341 7762fe12 2018-11-05 stsp return err;
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;
349 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
350 a440fac0 2018-09-06 stsp commit->nparents++;
352 a440fac0 2018-09-06 stsp return NULL;
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)
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;
362 a440fac0 2018-09-06 stsp *gmtoff = 0;
364 a440fac0 2018-09-06 stsp if (*p == '-')
366 a440fac0 2018-09-06 stsp else if (*p != '+')
367 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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'));
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');
380 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
381 a440fac0 2018-09-06 stsp return NULL;
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)
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;
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. */
405 9dbd8627 2021-02-04 stsp *gmtoff = 0;
406 9dbd8627 2021-02-04 stsp return NULL;
408 a440fac0 2018-09-06 stsp *space = '\0';
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);
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);
420 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
421 a440fac0 2018-09-06 stsp *space = '\0';
423 a440fac0 2018-09-06 stsp return NULL;
427 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
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)
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);
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)
446 45d799e2 2018-12-23 stsp return commit->tree_id;
450 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
452 45d799e2 2018-12-23 stsp return commit->nparents;
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)
458 45d799e2 2018-12-23 stsp return &commit->parent_ids;
461 45d799e2 2018-12-23 stsp const char *
462 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
464 45d799e2 2018-12-23 stsp return commit->author;
468 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
470 45d799e2 2018-12-23 stsp return commit->author_time;
473 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
475 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
478 45d799e2 2018-12-23 stsp const char *
479 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
481 45d799e2 2018-12-23 stsp return commit->committer;
485 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
487 45d799e2 2018-12-23 stsp return commit->committer_time;
491 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
493 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
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)
499 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
500 b9c41b54 2021-08-03 stsp const char *src;
502 5943eee2 2019-08-13 stsp size_t len;
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");
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.
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;
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;
532 b9c41b54 2021-08-03 stsp if (*src == '\n')
537 b9c41b54 2021-08-03 stsp *dst = '\0';
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);
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';
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';
559 5943eee2 2019-08-13 stsp free(*logmsg);
560 5943eee2 2019-08-13 stsp *logmsg = NULL;
562 5943eee2 2019-08-13 stsp return err;
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)
568 24ea5512 2019-08-22 stsp return commit->logmsg;
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)
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;
581 4793d91b 2019-09-22 stsp if (remain == 0)
582 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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");
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);
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);
600 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
601 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
603 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
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);
614 ff2a4428 2019-03-19 stsp s += label_len;
615 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
619 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
620 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
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) {
626 a440fac0 2018-09-06 stsp size_t slen;
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);
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);
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);
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");
650 a440fac0 2018-09-06 stsp s += slen + 1;
651 a440fac0 2018-09-06 stsp remain -= slen + 1;
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) {
657 a440fac0 2018-09-06 stsp size_t slen;
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);
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);
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);
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");
681 a440fac0 2018-09-06 stsp s += slen + 1;
682 a440fac0 2018-09-06 stsp remain -= slen + 1;
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");
692 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
693 a440fac0 2018-09-06 stsp *commit = NULL;
695 a440fac0 2018-09-06 stsp return err;
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)
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;
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;
714 3efd8e31 2022-10-23 thomas err = got_inflate_to_mem_fd(&p, &len, NULL, &csum, expected_size, fd);
716 3efd8e31 2022-10-23 thomas return err;
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;
724 3efd8e31 2022-10-23 thomas err = got_object_parse_header(&obj, p, len);
726 3efd8e31 2022-10-23 thomas goto done;
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;
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;
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);
744 3efd8e31 2022-10-23 thomas got_object_close(obj);
745 3efd8e31 2022-10-23 thomas return err;
749 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
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)
757 56e0773d 2019-11-28 stsp free(tree->entries);
758 03fa71c8 2018-09-06 stsp free(tree);
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)
765 8914529d 2019-04-13 stsp char *p, *space;
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);
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);
777 78e7b7b8 2022-05-19 thomas pte->mode = 0;
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';
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);
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;
798 78e7b7b8 2022-05-19 thomas static int
799 78e7b7b8 2022-05-19 thomas pte_cmp(const void *pa, const void *pb)
801 78e7b7b8 2022-05-19 thomas const struct got_parsed_tree_entry *a = pa, *b = pb;
803 78e7b7b8 2022-05-19 thomas return got_path_cmp(a->name, b->name, a->namelen, b->namelen);
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)
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;
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 */
820 a440fac0 2018-09-06 stsp while (remain > 0) {
821 a440fac0 2018-09-06 stsp size_t elen;
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;
830 78e7b7b8 2022-05-19 thomas *entries = pte;
831 c77e00b3 2022-10-18 thomas *nentries_alloc += nalloc;
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);
838 a440fac0 2018-09-06 stsp buf += elen;
839 a440fac0 2018-09-06 stsp remain -= elen;
840 3022d272 2019-11-14 stsp (*nentries)++;
843 a440fac0 2018-09-06 stsp if (remain != 0) {
844 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
848 78e7b7b8 2022-05-19 thomas if (*nentries > 1) {
849 78e7b7b8 2022-05-19 thomas mergesort(*entries, *nentries, sizeof(**entries), pte_cmp);
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);
863 3022d272 2019-11-14 stsp *nentries = 0;
864 f5d3d7af 2019-02-05 stsp return err;
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)
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;
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;
883 3efd8e31 2022-10-23 thomas err = got_inflate_to_mem_fd(p, &len, NULL, &csum, 0, fd);
885 3efd8e31 2022-10-23 thomas return err;
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;
893 3efd8e31 2022-10-23 thomas err = got_object_parse_header(&obj, *p, len);
895 3efd8e31 2022-10-23 thomas goto done;
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;
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);
908 3efd8e31 2022-10-23 thomas got_object_close(obj);
909 3efd8e31 2022-10-23 thomas return err;
913 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
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)
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);
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)
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;
936 4793d91b 2019-09-22 stsp if (remain == 0)
937 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
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");
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);
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);
955 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
956 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
958 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
962 f4a881ce 2018-11-17 stsp if (remain <= 0) {
963 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
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);
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;
1000 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1001 f4a881ce 2018-11-17 stsp goto done;
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;
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;
1015 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1016 f4a881ce 2018-11-17 stsp goto done;
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) {
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;
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;
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;
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;
1048 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1049 f4a881ce 2018-11-17 stsp goto done;
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) {
1055 f4a881ce 2018-11-17 stsp size_t slen;
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;
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;
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);
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;
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;
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;
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;
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;
1104 3efd8e31 2022-10-23 thomas return err;
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)
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;
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;
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;
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;
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;
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;
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);
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;