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 a440fac0 2018-09-06 stsp #include <sys/types.h>
18 a440fac0 2018-09-06 stsp #include <sys/stat.h>
19 cfe41121 2021-10-16 thomas #include <sys/queue.h>
20 cfe41121 2021-10-16 thomas #include <sys/tree.h>
21 a440fac0 2018-09-06 stsp #include <sys/uio.h>
22 a440fac0 2018-09-06 stsp #include <sys/socket.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
25 a440fac0 2018-09-06 stsp #include <errno.h>
26 a440fac0 2018-09-06 stsp #include <stdio.h>
27 a440fac0 2018-09-06 stsp #include <stdlib.h>
28 a440fac0 2018-09-06 stsp #include <string.h>
29 a440fac0 2018-09-06 stsp #include <stdint.h>
30 a440fac0 2018-09-06 stsp #include <zlib.h>
31 a440fac0 2018-09-06 stsp #include <ctype.h>
32 a440fac0 2018-09-06 stsp #include <limits.h>
33 a440fac0 2018-09-06 stsp #include <time.h>
34 ad242220 2018-09-08 stsp #include <unistd.h>
36 dd038bc6 2021-09-21 thomas.ad #include "got_compat.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 a440fac0 2018-09-06 stsp #include "got_lib_sha1.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 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
50 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
51 15a94983 2018-12-23 stsp #include "got_lib_privsep.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 ca6e02ac 2020-01-07 stsp struct got_object_id *
59 ca6e02ac 2020-01-07 stsp got_object_id_dup(struct got_object_id *id1)
61 ca6e02ac 2020-01-07 stsp struct got_object_id *id2;
63 ca6e02ac 2020-01-07 stsp id2 = malloc(sizeof(*id2));
64 ca6e02ac 2020-01-07 stsp if (id2 == NULL)
65 ca6e02ac 2020-01-07 stsp return NULL;
66 ca6e02ac 2020-01-07 stsp memcpy(id2, id1, sizeof(*id2));
71 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
72 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
74 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
77 2ff12563 2018-09-15 stsp const struct got_error *
78 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
80 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
82 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
83 5df4932d 2018-11-05 stsp if (*qid == NULL)
84 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
86 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
87 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
88 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
89 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
90 5df4932d 2018-11-05 stsp *qid = NULL;
93 74a2356f 2021-06-18 stsp (*qid)->data = NULL;
95 5df4932d 2018-11-05 stsp return NULL;
98 5df4932d 2018-11-05 stsp const struct got_error *
99 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
101 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
103 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
104 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
105 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
107 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
108 2ff12563 2018-09-15 stsp free(*outbuf);
109 2ff12563 2018-09-15 stsp *outbuf = NULL;
110 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
113 2ff12563 2018-09-15 stsp return NULL;
117 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
119 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
120 03fa71c8 2018-09-06 stsp obj->refcnt--;
121 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
125 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
126 03fa71c8 2018-09-06 stsp struct got_delta *delta;
127 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&obj->deltas.entries)) {
128 dbdddfee 2021-06-23 naddy delta = STAILQ_FIRST(&obj->deltas.entries);
129 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&obj->deltas.entries, entry);
130 2256993b 2019-07-15 stsp free(delta);
136 8ab9215c 2021-10-15 thomas const struct got_error *
137 8ab9215c 2021-10-15 thomas got_object_raw_close(struct got_raw_object *obj)
139 8ab9215c 2021-10-15 thomas const struct got_error *err = NULL;
141 8ab9215c 2021-10-15 thomas if (obj->refcnt > 0) {
142 8ab9215c 2021-10-15 thomas obj->refcnt--;
143 8ab9215c 2021-10-15 thomas if (obj->refcnt > 0)
144 8ab9215c 2021-10-15 thomas return NULL;
147 c565dfd3 2021-10-16 thomas free(obj->read_buf);
148 8ab9215c 2021-10-15 thomas if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
149 8ab9215c 2021-10-15 thomas err = got_error_from_errno("fclose");
150 8ab9215c 2021-10-15 thomas free(obj->data);
151 8ab9215c 2021-10-15 thomas free(obj);
152 8ab9215c 2021-10-15 thomas return err;
156 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
158 03fa71c8 2018-09-06 stsp free(qid->id);
163 dd88155e 2019-06-29 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
165 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
167 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(ids)) {
168 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(ids);
169 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(ids, entry);
170 dd88155e 2019-06-29 stsp got_object_qid_free(qid);
174 1785f84a 2018-12-23 stsp const struct got_error *
175 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
177 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
178 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
179 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
180 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
181 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
183 1785f84a 2018-12-23 stsp const int obj_types[] = {
184 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
185 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
186 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
187 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
189 1785f84a 2018-12-23 stsp int type = 0;
190 1785f84a 2018-12-23 stsp size_t size = 0, hdrlen = 0;
193 1785f84a 2018-12-23 stsp *obj = NULL;
195 9ef4ac16 2019-04-13 stsp hdrlen = strnlen(buf, len) + 1 /* '\0' */;
196 9ef4ac16 2019-04-13 stsp if (hdrlen > len)
197 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
199 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
200 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
201 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
202 1785f84a 2018-12-23 stsp const char *errstr;
204 ff2a4428 2019-03-19 stsp if (strncmp(buf, label, label_len) != 0)
207 1785f84a 2018-12-23 stsp type = obj_types[i];
208 ff2a4428 2019-03-19 stsp if (len <= label_len)
209 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
210 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
211 1785f84a 2018-12-23 stsp if (errstr != NULL)
212 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
216 1785f84a 2018-12-23 stsp if (type == 0)
217 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
219 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
220 1785f84a 2018-12-23 stsp if (*obj == NULL)
221 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
222 1785f84a 2018-12-23 stsp (*obj)->type = type;
223 1785f84a 2018-12-23 stsp (*obj)->hdrlen = hdrlen;
224 1785f84a 2018-12-23 stsp (*obj)->size = size;
225 1785f84a 2018-12-23 stsp return NULL;
228 1785f84a 2018-12-23 stsp const struct got_error *
229 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
231 1785f84a 2018-12-23 stsp const struct got_error *err;
232 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
233 dbaa2362 2021-09-28 thomas uint8_t *buf;
234 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
235 1785f84a 2018-12-23 stsp size_t outlen, totlen;
236 1785f84a 2018-12-23 stsp int nbuf = 1;
238 1785f84a 2018-12-23 stsp *obj = NULL;
240 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
241 1785f84a 2018-12-23 stsp if (buf == NULL)
242 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
244 1e87a3c3 2020-03-18 stsp err = got_inflate_init(&zb, buf, zbsize, NULL);
246 1785f84a 2018-12-23 stsp return err;
248 1785f84a 2018-12-23 stsp totlen = 0;
250 3ab5e33c 2020-03-18 stsp err = got_inflate_read_fd(&zb, fd, &outlen, NULL);
253 1785f84a 2018-12-23 stsp if (outlen == 0)
255 1785f84a 2018-12-23 stsp totlen += outlen;
256 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
257 dbaa2362 2021-09-28 thomas uint8_t *newbuf;
259 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
260 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
261 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
264 1785f84a 2018-12-23 stsp buf = newbuf;
265 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
266 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
268 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
270 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
273 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
274 1785f84a 2018-12-23 stsp return err;
277 a440fac0 2018-09-06 stsp struct got_commit_object *
278 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
280 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
282 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
283 a440fac0 2018-09-06 stsp if (commit == NULL)
284 a440fac0 2018-09-06 stsp return NULL;
285 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
286 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
287 a440fac0 2018-09-06 stsp free(commit);
288 a440fac0 2018-09-06 stsp return NULL;
291 dbdddfee 2021-06-23 naddy STAILQ_INIT(&commit->parent_ids);
293 a440fac0 2018-09-06 stsp return commit;
296 a440fac0 2018-09-06 stsp const struct got_error *
297 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
298 a440fac0 2018-09-06 stsp const char *id_str)
300 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
301 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
303 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
305 7762fe12 2018-11-05 stsp return err;
307 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
308 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
309 00eb6a1f 2019-07-15 stsp got_object_qid_free(qid);
310 a440fac0 2018-09-06 stsp return err;
313 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
314 a440fac0 2018-09-06 stsp commit->nparents++;
316 a440fac0 2018-09-06 stsp return NULL;
319 a440fac0 2018-09-06 stsp static const struct got_error *
320 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
322 a440fac0 2018-09-06 stsp int sign = 1;
323 a440fac0 2018-09-06 stsp const char *p = tzstr;
324 a440fac0 2018-09-06 stsp time_t h, m;
326 a440fac0 2018-09-06 stsp *gmtoff = 0;
328 a440fac0 2018-09-06 stsp if (*p == '-')
330 a440fac0 2018-09-06 stsp else if (*p != '+')
331 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
333 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
334 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
335 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
338 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
339 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
340 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
342 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
343 a440fac0 2018-09-06 stsp return NULL;
346 a440fac0 2018-09-06 stsp static const struct got_error *
347 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
349 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
350 a440fac0 2018-09-06 stsp const char *errstr;
351 a440fac0 2018-09-06 stsp char *space, *tzstr;
353 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
354 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
355 a440fac0 2018-09-06 stsp if (space == NULL)
356 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
357 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
358 a440fac0 2018-09-06 stsp if (tzstr == NULL)
359 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
360 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
361 a440fac0 2018-09-06 stsp free(tzstr);
363 9dbd8627 2021-02-04 stsp if (err->code != GOT_ERR_BAD_OBJ_DATA)
364 9dbd8627 2021-02-04 stsp return err;
365 9dbd8627 2021-02-04 stsp /* Old versions of Git omitted the timestamp. */
367 9dbd8627 2021-02-04 stsp *gmtoff = 0;
368 9dbd8627 2021-02-04 stsp return NULL;
370 a440fac0 2018-09-06 stsp *space = '\0';
372 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
373 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
374 a440fac0 2018-09-06 stsp if (space == NULL)
375 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
377 09867e48 2019-08-13 stsp /* Timestamp parsed here is expressed as UNIX timestamp (UTC). */
378 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
379 a440fac0 2018-09-06 stsp if (errstr)
380 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
382 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
383 a440fac0 2018-09-06 stsp *space = '\0';
385 a440fac0 2018-09-06 stsp return NULL;
389 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
391 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
392 03fa71c8 2018-09-06 stsp commit->refcnt--;
393 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
397 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
398 03fa71c8 2018-09-06 stsp free(commit->tree_id);
399 03fa71c8 2018-09-06 stsp free(commit->author);
400 03fa71c8 2018-09-06 stsp free(commit->committer);
401 03fa71c8 2018-09-06 stsp free(commit->logmsg);
402 03fa71c8 2018-09-06 stsp free(commit);
405 45d799e2 2018-12-23 stsp struct got_object_id *
406 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
408 45d799e2 2018-12-23 stsp return commit->tree_id;
412 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
414 45d799e2 2018-12-23 stsp return commit->nparents;
417 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
418 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
420 45d799e2 2018-12-23 stsp return &commit->parent_ids;
423 45d799e2 2018-12-23 stsp const char *
424 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
426 45d799e2 2018-12-23 stsp return commit->author;
430 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
432 45d799e2 2018-12-23 stsp return commit->author_time;
435 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
437 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
440 45d799e2 2018-12-23 stsp const char *
441 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
443 45d799e2 2018-12-23 stsp return commit->committer;
447 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
449 45d799e2 2018-12-23 stsp return commit->committer_time;
453 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
455 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
458 5943eee2 2019-08-13 stsp const struct got_error *
459 5943eee2 2019-08-13 stsp got_object_commit_get_logmsg(char **logmsg, struct got_commit_object *commit)
461 5943eee2 2019-08-13 stsp const struct got_error *err = NULL;
462 b9c41b54 2021-08-03 stsp const char *src;
464 5943eee2 2019-08-13 stsp size_t len;
466 b9c41b54 2021-08-03 stsp len = strlen(commit->logmsg);
467 b9c41b54 2021-08-03 stsp *logmsg = malloc(len + 2); /* leave room for a trailing \n and \0 */
468 b9c41b54 2021-08-03 stsp if (*logmsg == NULL)
469 b9c41b54 2021-08-03 stsp return got_error_from_errno("malloc");
472 b9c41b54 2021-08-03 stsp * Strip out unusual headers. Headers are separated from the commit
473 b9c41b54 2021-08-03 stsp * message body by a single empty line.
475 b9c41b54 2021-08-03 stsp src = commit->logmsg;
476 b9c41b54 2021-08-03 stsp dst = *logmsg;
477 b9c41b54 2021-08-03 stsp while (*src != '\0' && *src != '\n') {
478 b9c41b54 2021-08-03 stsp int copy_header = 1, eol = 0;
479 b9c41b54 2021-08-03 stsp if (strncmp(src, GOT_COMMIT_LABEL_TREE,
480 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_TREE)) != 0 &&
481 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_AUTHOR,
482 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_AUTHOR)) != 0 &&
483 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_PARENT,
484 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_PARENT)) != 0 &&
485 b9c41b54 2021-08-03 stsp strncmp(src, GOT_COMMIT_LABEL_COMMITTER,
486 b9c41b54 2021-08-03 stsp strlen(GOT_COMMIT_LABEL_COMMITTER)) != 0)
487 b9c41b54 2021-08-03 stsp copy_header = 0;
489 b9c41b54 2021-08-03 stsp while (*src != '\0' && !eol) {
490 b9c41b54 2021-08-03 stsp if (copy_header) {
491 b9c41b54 2021-08-03 stsp *dst = *src;
494 b9c41b54 2021-08-03 stsp if (*src == '\n')
499 b9c41b54 2021-08-03 stsp *dst = '\0';
501 b9c41b54 2021-08-03 stsp if (strlcat(*logmsg, src, len + 1) >= len + 1) {
502 b9c41b54 2021-08-03 stsp err = got_error(GOT_ERR_NO_SPACE);
506 5943eee2 2019-08-13 stsp /* Trim redundant trailing whitespace. */
507 5943eee2 2019-08-13 stsp len = strlen(*logmsg);
508 5943eee2 2019-08-13 stsp while (len > 1 && isspace((unsigned char)(*logmsg)[len - 2]) &&
509 5943eee2 2019-08-13 stsp isspace((unsigned char)(*logmsg)[len - 1])) {
510 5943eee2 2019-08-13 stsp (*logmsg)[len - 1] = '\0';
514 b9c41b54 2021-08-03 stsp /* Append a trailing newline if missing. */
515 b9c41b54 2021-08-03 stsp if (len > 0 && (*logmsg)[len - 1] != '\n') {
516 b9c41b54 2021-08-03 stsp (*logmsg)[len] = '\n';
517 b9c41b54 2021-08-03 stsp (*logmsg)[len + 1] = '\0';
521 5943eee2 2019-08-13 stsp free(*logmsg);
522 5943eee2 2019-08-13 stsp *logmsg = NULL;
524 5943eee2 2019-08-13 stsp return err;
527 24ea5512 2019-08-22 stsp const char *
528 24ea5512 2019-08-22 stsp got_object_commit_get_logmsg_raw(struct got_commit_object *commit)
530 24ea5512 2019-08-22 stsp return commit->logmsg;
533 a440fac0 2018-09-06 stsp const struct got_error *
534 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
535 5e0b25c4 2018-12-24 stsp size_t len)
537 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
538 a440fac0 2018-09-06 stsp char *s = buf;
539 ff2a4428 2019-03-19 stsp size_t label_len;
540 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
542 4793d91b 2019-09-22 stsp if (remain == 0)
543 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
545 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
546 a440fac0 2018-09-06 stsp if (*commit == NULL)
547 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
549 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
550 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
551 ff2a4428 2019-03-19 stsp remain -= label_len;
552 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
553 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
556 ff2a4428 2019-03-19 stsp s += label_len;
557 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
558 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
561 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
562 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
564 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
568 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
569 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
570 ff2a4428 2019-03-19 stsp remain -= label_len;
571 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
572 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
575 ff2a4428 2019-03-19 stsp s += label_len;
576 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
580 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
581 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
584 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
585 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
587 a440fac0 2018-09-06 stsp size_t slen;
589 ff2a4428 2019-03-19 stsp remain -= label_len;
590 a440fac0 2018-09-06 stsp if (remain <= 0) {
591 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
594 ff2a4428 2019-03-19 stsp s += label_len;
595 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
596 a440fac0 2018-09-06 stsp if (p == NULL) {
597 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
601 a440fac0 2018-09-06 stsp slen = strlen(s);
602 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
603 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
606 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
607 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
608 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
611 a440fac0 2018-09-06 stsp s += slen + 1;
612 a440fac0 2018-09-06 stsp remain -= slen + 1;
615 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
616 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
618 a440fac0 2018-09-06 stsp size_t slen;
620 ff2a4428 2019-03-19 stsp remain -= label_len;
621 a440fac0 2018-09-06 stsp if (remain <= 0) {
622 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 ff2a4428 2019-03-19 stsp s += label_len;
626 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
627 a440fac0 2018-09-06 stsp if (p == NULL) {
628 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 a440fac0 2018-09-06 stsp slen = strlen(s);
633 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
634 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
637 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
638 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
639 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
642 a440fac0 2018-09-06 stsp s += slen + 1;
643 a440fac0 2018-09-06 stsp remain -= slen + 1;
646 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
647 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
648 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
653 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
654 a440fac0 2018-09-06 stsp *commit = NULL;
656 a440fac0 2018-09-06 stsp return err;
660 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
662 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
663 03fa71c8 2018-09-06 stsp tree->refcnt--;
664 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
668 56e0773d 2019-11-28 stsp free(tree->entries);
669 03fa71c8 2018-09-06 stsp free(tree);
672 a440fac0 2018-09-06 stsp static const struct got_error *
673 3022d272 2019-11-14 stsp parse_tree_entry(struct got_parsed_tree_entry **pte, const char **name,
674 3022d272 2019-11-14 stsp size_t *elen, char *buf,
675 a440fac0 2018-09-06 stsp size_t maxlen)
677 8914529d 2019-04-13 stsp char *p, *space;
678 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
680 3022d272 2019-11-14 stsp *name = NULL;
683 3022d272 2019-11-14 stsp *pte = malloc(sizeof(**pte));
684 3022d272 2019-11-14 stsp if (*pte == NULL)
685 3022d272 2019-11-14 stsp return got_error_from_errno("malloc");
687 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
688 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
689 3022d272 2019-11-14 stsp free(*pte);
690 3022d272 2019-11-14 stsp *pte = NULL;
691 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
694 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
695 8914529d 2019-04-13 stsp if (space == NULL || space <= buf) {
696 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
697 3022d272 2019-11-14 stsp free(*pte);
698 3022d272 2019-11-14 stsp *pte = NULL;
699 a440fac0 2018-09-06 stsp return err;
701 3022d272 2019-11-14 stsp (*pte)->mode = 0;
703 8914529d 2019-04-13 stsp while (p < space) {
704 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
705 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
708 3022d272 2019-11-14 stsp (*pte)->mode <<= 3;
709 3022d272 2019-11-14 stsp (*pte)->mode |= *p - '0';
713 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
714 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
717 3022d272 2019-11-14 stsp *name = space + 1;
718 68bf1b1e 2018-11-07 stsp buf += *elen;
719 3022d272 2019-11-14 stsp (*pte)->id = buf;
720 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
723 3022d272 2019-11-14 stsp free(*pte);
724 3022d272 2019-11-14 stsp *pte = NULL;
726 a440fac0 2018-09-06 stsp return err;
729 a440fac0 2018-09-06 stsp const struct got_error *
730 3022d272 2019-11-14 stsp got_object_parse_tree(struct got_pathlist_head *entries, int *nentries,
731 3022d272 2019-11-14 stsp uint8_t *buf, size_t len)
733 3022d272 2019-11-14 stsp const struct got_error *err = NULL;
734 a440fac0 2018-09-06 stsp size_t remain = len;
736 3022d272 2019-11-14 stsp *nentries = 0;
737 db1d3576 2019-10-04 stsp if (remain == 0)
738 db1d3576 2019-10-04 stsp return NULL; /* tree is empty */
740 a440fac0 2018-09-06 stsp while (remain > 0) {
741 3022d272 2019-11-14 stsp struct got_parsed_tree_entry *pte;
742 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
743 3022d272 2019-11-14 stsp const char *name;
744 a440fac0 2018-09-06 stsp size_t elen;
746 3022d272 2019-11-14 stsp err = parse_tree_entry(&pte, &name, &elen, buf, remain);
749 3022d272 2019-11-14 stsp err = got_pathlist_insert(&new, entries, name, pte);
752 f5d3d7af 2019-02-05 stsp if (new == NULL) {
753 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
756 a440fac0 2018-09-06 stsp buf += elen;
757 a440fac0 2018-09-06 stsp remain -= elen;
758 3022d272 2019-11-14 stsp (*nentries)++;
761 a440fac0 2018-09-06 stsp if (remain != 0) {
762 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
767 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(entries);
768 3022d272 2019-11-14 stsp *nentries = 0;
770 f5d3d7af 2019-02-05 stsp return err;
774 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(struct got_pathlist_head *entries)
776 b64b1f95 2020-01-06 stsp struct got_pathlist_entry *pe;
778 b64b1f95 2020-01-06 stsp TAILQ_FOREACH(pe, entries, entry) {
779 b64b1f95 2020-01-06 stsp struct got_parsed_tree_entry *pte = pe->data;
782 b64b1f95 2020-01-06 stsp got_pathlist_free(entries);
786 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
788 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0) {
789 ca0d469c 2019-08-13 stsp tag->refcnt--;
790 ca0d469c 2019-08-13 stsp if (tag->refcnt > 0)
794 f4a881ce 2018-11-17 stsp free(tag->tag);
795 f4a881ce 2018-11-17 stsp free(tag->tagger);
796 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
800 ad242220 2018-09-08 stsp const struct got_error *
801 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
803 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
804 f4a881ce 2018-11-17 stsp size_t remain = len;
805 f4a881ce 2018-11-17 stsp char *s = buf;
806 ff2a4428 2019-03-19 stsp size_t label_len;
808 4793d91b 2019-09-22 stsp if (remain == 0)
809 4793d91b 2019-09-22 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
811 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
812 f4a881ce 2018-11-17 stsp if (*tag == NULL)
813 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
815 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
816 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
817 ff2a4428 2019-03-19 stsp remain -= label_len;
818 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
819 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
822 ff2a4428 2019-03-19 stsp s += label_len;
823 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
824 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
827 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
828 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
830 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
834 f4a881ce 2018-11-17 stsp if (remain <= 0) {
835 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
839 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
840 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
841 ff2a4428 2019-03-19 stsp remain -= label_len;
842 f4a881ce 2018-11-17 stsp if (remain <= 0) {
843 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
846 ff2a4428 2019-03-19 stsp s += label_len;
847 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
848 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
849 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
850 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
851 ff2a4428 2019-03-19 stsp s += label_len;
852 ff2a4428 2019-03-19 stsp remain -= label_len;
853 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
854 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
855 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
856 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
857 ff2a4428 2019-03-19 stsp s += label_len;
858 ff2a4428 2019-03-19 stsp remain -= label_len;
859 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
860 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
861 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
862 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
863 ff2a4428 2019-03-19 stsp s += label_len;
864 ff2a4428 2019-03-19 stsp remain -= label_len;
865 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
866 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
867 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
868 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
869 ff2a4428 2019-03-19 stsp s += label_len;
870 ff2a4428 2019-03-19 stsp remain -= label_len;
872 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
876 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
877 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
882 f4a881ce 2018-11-17 stsp if (remain <= 0) {
883 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
887 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
891 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
892 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
894 f4a881ce 2018-11-17 stsp size_t slen;
895 ff2a4428 2019-03-19 stsp remain -= label_len;
896 f4a881ce 2018-11-17 stsp if (remain <= 0) {
897 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
900 ff2a4428 2019-03-19 stsp s += label_len;
901 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
902 f4a881ce 2018-11-17 stsp if (p == NULL) {
903 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
907 f4a881ce 2018-11-17 stsp slen = strlen(s);
908 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
909 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
910 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
913 f4a881ce 2018-11-17 stsp s += slen + 1;
914 f4a881ce 2018-11-17 stsp remain -= slen + 1;
915 f4a881ce 2018-11-17 stsp if (remain <= 0) {
916 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
920 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
924 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
925 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
927 f4a881ce 2018-11-17 stsp size_t slen;
929 ff2a4428 2019-03-19 stsp remain -= label_len;
930 f4a881ce 2018-11-17 stsp if (remain <= 0) {
931 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
934 ff2a4428 2019-03-19 stsp s += label_len;
935 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
936 f4a881ce 2018-11-17 stsp if (p == NULL) {
937 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
941 f4a881ce 2018-11-17 stsp slen = strlen(s);
942 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
943 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
946 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
947 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
948 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
951 f4a881ce 2018-11-17 stsp s += slen + 1;
952 f4a881ce 2018-11-17 stsp remain -= slen + 1;
953 5a8b373c 2020-12-18 stsp if (remain < 0) {
954 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
958 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
959 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
960 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
961 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
966 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
967 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
968 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
973 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
974 f4a881ce 2018-11-17 stsp *tag = NULL;
976 f4a881ce 2018-11-17 stsp return err;
979 f4a881ce 2018-11-17 stsp const struct got_error *
980 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
982 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
983 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
984 a440fac0 2018-09-06 stsp size_t n, total, remain;
985 a440fac0 2018-09-06 stsp uint8_t *buf;
987 a440fac0 2018-09-06 stsp *outbuf = NULL;
988 a440fac0 2018-09-06 stsp *outlen = 0;
990 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
991 a440fac0 2018-09-06 stsp if (buf == NULL)
992 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
994 a440fac0 2018-09-06 stsp remain = blocksize;
997 a440fac0 2018-09-06 stsp if (remain == 0) {
998 a440fac0 2018-09-06 stsp uint8_t *newbuf;
999 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
1000 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
1001 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
1002 a440fac0 2018-09-06 stsp goto done;
1004 a440fac0 2018-09-06 stsp buf = newbuf;
1005 a440fac0 2018-09-06 stsp remain += blocksize;
1007 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
1008 a440fac0 2018-09-06 stsp if (n == 0) {
1009 a440fac0 2018-09-06 stsp if (ferror(f)) {
1010 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
1011 a440fac0 2018-09-06 stsp goto done;
1013 a440fac0 2018-09-06 stsp break; /* EOF */
1015 a440fac0 2018-09-06 stsp remain -= n;
1016 a440fac0 2018-09-06 stsp total += n;
1020 a440fac0 2018-09-06 stsp if (err == NULL) {
1021 a440fac0 2018-09-06 stsp *outbuf = buf;
1022 a440fac0 2018-09-06 stsp *outlen = total;
1024 a440fac0 2018-09-06 stsp free(buf);
1025 ad242220 2018-09-08 stsp return err;