Blame


1 a440fac0 2018-09-06 stsp /*
2 fae40e36 2019-04-13 stsp * Copyright (c) 2018, 2019 Stefan Sperling <stsp@openbsd.org>
3 a440fac0 2018-09-06 stsp *
4 a440fac0 2018-09-06 stsp * Permission to use, copy, modify, and distribute this software for any
5 a440fac0 2018-09-06 stsp * purpose with or without fee is hereby granted, provided that the above
6 a440fac0 2018-09-06 stsp * copyright notice and this permission notice appear in all copies.
7 a440fac0 2018-09-06 stsp *
8 a440fac0 2018-09-06 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a440fac0 2018-09-06 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a440fac0 2018-09-06 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a440fac0 2018-09-06 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a440fac0 2018-09-06 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a440fac0 2018-09-06 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a440fac0 2018-09-06 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 a440fac0 2018-09-06 stsp */
16 a440fac0 2018-09-06 stsp
17 a440fac0 2018-09-06 stsp #include <sys/types.h>
18 a440fac0 2018-09-06 stsp #include <sys/stat.h>
19 a440fac0 2018-09-06 stsp #include <sys/queue.h>
20 a440fac0 2018-09-06 stsp #include <sys/uio.h>
21 a440fac0 2018-09-06 stsp #include <sys/socket.h>
22 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
23 a440fac0 2018-09-06 stsp #include <sys/wait.h>
24 a440fac0 2018-09-06 stsp
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 <sha1.h>
31 a440fac0 2018-09-06 stsp #include <zlib.h>
32 a440fac0 2018-09-06 stsp #include <ctype.h>
33 a440fac0 2018-09-06 stsp #include <limits.h>
34 a440fac0 2018-09-06 stsp #include <imsg.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 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 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
49 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
50 15a94983 2018-12-23 stsp #include "got_lib_privsep.h"
51 ad242220 2018-09-08 stsp #include "got_lib_repository.h"
52 a440fac0 2018-09-06 stsp
53 a440fac0 2018-09-06 stsp #ifndef nitems
54 a440fac0 2018-09-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
55 a440fac0 2018-09-06 stsp #endif
56 a440fac0 2018-09-06 stsp
57 f054b67a 2018-11-05 stsp int
58 f054b67a 2018-11-05 stsp got_object_id_cmp(const struct got_object_id *id1,
59 f054b67a 2018-11-05 stsp const struct got_object_id *id2)
60 f054b67a 2018-11-05 stsp {
61 f054b67a 2018-11-05 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
62 f054b67a 2018-11-05 stsp }
63 f054b67a 2018-11-05 stsp
64 2ff12563 2018-09-15 stsp const struct got_error *
65 5df4932d 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
66 5df4932d 2018-11-05 stsp {
67 5df4932d 2018-11-05 stsp const struct got_error *err = NULL;
68 5df4932d 2018-11-05 stsp
69 5df4932d 2018-11-05 stsp *qid = malloc(sizeof(**qid));
70 5df4932d 2018-11-05 stsp if (*qid == NULL)
71 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
72 5df4932d 2018-11-05 stsp
73 5df4932d 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
74 5df4932d 2018-11-05 stsp if ((*qid)->id == NULL) {
75 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
76 5df4932d 2018-11-05 stsp got_object_qid_free(*qid);
77 5df4932d 2018-11-05 stsp *qid = NULL;
78 5df4932d 2018-11-05 stsp return err;
79 5df4932d 2018-11-05 stsp }
80 5df4932d 2018-11-05 stsp
81 5df4932d 2018-11-05 stsp return NULL;
82 5df4932d 2018-11-05 stsp }
83 5df4932d 2018-11-05 stsp
84 5df4932d 2018-11-05 stsp const struct got_error *
85 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
86 2ff12563 2018-09-15 stsp {
87 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
88 2ff12563 2018-09-15 stsp
89 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
90 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
91 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
92 2ff12563 2018-09-15 stsp
93 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
94 2ff12563 2018-09-15 stsp free(*outbuf);
95 2ff12563 2018-09-15 stsp *outbuf = NULL;
96 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
97 2ff12563 2018-09-15 stsp }
98 a440fac0 2018-09-06 stsp
99 2ff12563 2018-09-15 stsp return NULL;
100 2ff12563 2018-09-15 stsp }
101 2ff12563 2018-09-15 stsp
102 03fa71c8 2018-09-06 stsp void
103 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
104 03fa71c8 2018-09-06 stsp {
105 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
106 03fa71c8 2018-09-06 stsp obj->refcnt--;
107 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
108 03fa71c8 2018-09-06 stsp return;
109 03fa71c8 2018-09-06 stsp }
110 03fa71c8 2018-09-06 stsp
111 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
112 03fa71c8 2018-09-06 stsp struct got_delta *delta;
113 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
114 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
115 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
116 03fa71c8 2018-09-06 stsp got_delta_close(delta);
117 03fa71c8 2018-09-06 stsp }
118 03fa71c8 2018-09-06 stsp }
119 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
120 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
121 03fa71c8 2018-09-06 stsp free(obj);
122 03fa71c8 2018-09-06 stsp }
123 03fa71c8 2018-09-06 stsp
124 03fa71c8 2018-09-06 stsp void
125 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
126 03fa71c8 2018-09-06 stsp {
127 03fa71c8 2018-09-06 stsp free(qid->id);
128 03fa71c8 2018-09-06 stsp free(qid);
129 1785f84a 2018-12-23 stsp }
130 1785f84a 2018-12-23 stsp
131 dd88155e 2019-06-29 stsp void
132 dd88155e 2019-06-29 stsp got_object_id_queue_free(struct got_object_id_queue *ids)
133 dd88155e 2019-06-29 stsp {
134 dd88155e 2019-06-29 stsp struct got_object_qid *qid;
135 dd88155e 2019-06-29 stsp
136 dd88155e 2019-06-29 stsp while (!SIMPLEQ_EMPTY(ids)) {
137 dd88155e 2019-06-29 stsp qid = SIMPLEQ_FIRST(ids);
138 dd88155e 2019-06-29 stsp SIMPLEQ_REMOVE_HEAD(ids, entry);
139 dd88155e 2019-06-29 stsp got_object_qid_free(qid);
140 dd88155e 2019-06-29 stsp }
141 dd88155e 2019-06-29 stsp }
142 dd88155e 2019-06-29 stsp
143 1785f84a 2018-12-23 stsp const struct got_error *
144 1785f84a 2018-12-23 stsp got_object_parse_header(struct got_object **obj, char *buf, size_t len)
145 1785f84a 2018-12-23 stsp {
146 ff2a4428 2019-03-19 stsp const char *obj_labels[] = {
147 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_COMMIT,
148 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TREE,
149 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_BLOB,
150 ff2a4428 2019-03-19 stsp GOT_OBJ_LABEL_TAG,
151 1785f84a 2018-12-23 stsp };
152 1785f84a 2018-12-23 stsp const int obj_types[] = {
153 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_COMMIT,
154 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TREE,
155 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_BLOB,
156 1785f84a 2018-12-23 stsp GOT_OBJ_TYPE_TAG,
157 1785f84a 2018-12-23 stsp };
158 1785f84a 2018-12-23 stsp int type = 0;
159 1785f84a 2018-12-23 stsp size_t size = 0, hdrlen = 0;
160 1785f84a 2018-12-23 stsp int i;
161 1785f84a 2018-12-23 stsp
162 1785f84a 2018-12-23 stsp *obj = NULL;
163 1785f84a 2018-12-23 stsp
164 9ef4ac16 2019-04-13 stsp hdrlen = strnlen(buf, len) + 1 /* '\0' */;
165 9ef4ac16 2019-04-13 stsp if (hdrlen > len)
166 9ef4ac16 2019-04-13 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
167 1785f84a 2018-12-23 stsp
168 ff2a4428 2019-03-19 stsp for (i = 0; i < nitems(obj_labels); i++) {
169 ff2a4428 2019-03-19 stsp const char *label = obj_labels[i];
170 ff2a4428 2019-03-19 stsp size_t label_len = strlen(label);
171 1785f84a 2018-12-23 stsp const char *errstr;
172 1785f84a 2018-12-23 stsp
173 ff2a4428 2019-03-19 stsp if (strncmp(buf, label, label_len) != 0)
174 1785f84a 2018-12-23 stsp continue;
175 1785f84a 2018-12-23 stsp
176 1785f84a 2018-12-23 stsp type = obj_types[i];
177 ff2a4428 2019-03-19 stsp if (len <= label_len)
178 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
179 ff2a4428 2019-03-19 stsp size = strtonum(buf + label_len, 0, LONG_MAX, &errstr);
180 1785f84a 2018-12-23 stsp if (errstr != NULL)
181 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
182 1785f84a 2018-12-23 stsp break;
183 1785f84a 2018-12-23 stsp }
184 1785f84a 2018-12-23 stsp
185 1785f84a 2018-12-23 stsp if (type == 0)
186 1785f84a 2018-12-23 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
187 1785f84a 2018-12-23 stsp
188 1785f84a 2018-12-23 stsp *obj = calloc(1, sizeof(**obj));
189 1785f84a 2018-12-23 stsp if (*obj == NULL)
190 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
191 1785f84a 2018-12-23 stsp (*obj)->type = type;
192 1785f84a 2018-12-23 stsp (*obj)->hdrlen = hdrlen;
193 1785f84a 2018-12-23 stsp (*obj)->size = size;
194 1785f84a 2018-12-23 stsp return NULL;
195 1785f84a 2018-12-23 stsp }
196 1785f84a 2018-12-23 stsp
197 1785f84a 2018-12-23 stsp const struct got_error *
198 1785f84a 2018-12-23 stsp got_object_read_header(struct got_object **obj, int fd)
199 1785f84a 2018-12-23 stsp {
200 1785f84a 2018-12-23 stsp const struct got_error *err;
201 23bc48a9 2019-03-19 stsp struct got_inflate_buf zb;
202 1785f84a 2018-12-23 stsp char *buf;
203 1785f84a 2018-12-23 stsp const size_t zbsize = 64;
204 1785f84a 2018-12-23 stsp size_t outlen, totlen;
205 1785f84a 2018-12-23 stsp int nbuf = 1;
206 1785f84a 2018-12-23 stsp
207 1785f84a 2018-12-23 stsp *obj = NULL;
208 1785f84a 2018-12-23 stsp
209 1785f84a 2018-12-23 stsp buf = malloc(zbsize);
210 1785f84a 2018-12-23 stsp if (buf == NULL)
211 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
212 1785f84a 2018-12-23 stsp
213 1785f84a 2018-12-23 stsp err = got_inflate_init(&zb, buf, zbsize);
214 1785f84a 2018-12-23 stsp if (err)
215 1785f84a 2018-12-23 stsp return err;
216 1785f84a 2018-12-23 stsp
217 1785f84a 2018-12-23 stsp totlen = 0;
218 1785f84a 2018-12-23 stsp do {
219 1785f84a 2018-12-23 stsp err = got_inflate_read_fd(&zb, fd, &outlen);
220 1785f84a 2018-12-23 stsp if (err)
221 1785f84a 2018-12-23 stsp goto done;
222 1785f84a 2018-12-23 stsp if (outlen == 0)
223 1785f84a 2018-12-23 stsp break;
224 1785f84a 2018-12-23 stsp totlen += outlen;
225 dedbbd9d 2019-04-13 stsp if (memchr(zb.outbuf, '\0', outlen) == NULL) {
226 1785f84a 2018-12-23 stsp char *newbuf;
227 1785f84a 2018-12-23 stsp nbuf++;
228 1785f84a 2018-12-23 stsp newbuf = recallocarray(buf, nbuf - 1, nbuf, zbsize);
229 1785f84a 2018-12-23 stsp if (newbuf == NULL) {
230 638f9024 2019-05-13 stsp err = got_error_from_errno("recallocarray");
231 1785f84a 2018-12-23 stsp goto done;
232 1785f84a 2018-12-23 stsp }
233 1785f84a 2018-12-23 stsp buf = newbuf;
234 1785f84a 2018-12-23 stsp zb.outbuf = newbuf + totlen;
235 1785f84a 2018-12-23 stsp zb.outlen = (nbuf * zbsize) - totlen;
236 1785f84a 2018-12-23 stsp }
237 dedbbd9d 2019-04-13 stsp } while (memchr(zb.outbuf, '\0', outlen) == NULL);
238 1785f84a 2018-12-23 stsp
239 1785f84a 2018-12-23 stsp err = got_object_parse_header(obj, buf, totlen);
240 1785f84a 2018-12-23 stsp done:
241 1785f84a 2018-12-23 stsp free(buf);
242 1785f84a 2018-12-23 stsp got_inflate_end(&zb);
243 1785f84a 2018-12-23 stsp return err;
244 876c234b 2018-09-10 stsp }
245 876c234b 2018-09-10 stsp
246 a440fac0 2018-09-06 stsp struct got_commit_object *
247 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
248 a440fac0 2018-09-06 stsp {
249 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
250 a440fac0 2018-09-06 stsp
251 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
252 a440fac0 2018-09-06 stsp if (commit == NULL)
253 a440fac0 2018-09-06 stsp return NULL;
254 acf0c7c6 2018-11-05 stsp commit->tree_id = malloc(sizeof(*commit->tree_id));
255 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
256 a440fac0 2018-09-06 stsp free(commit);
257 a440fac0 2018-09-06 stsp return NULL;
258 a440fac0 2018-09-06 stsp }
259 a440fac0 2018-09-06 stsp
260 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
261 a440fac0 2018-09-06 stsp
262 a440fac0 2018-09-06 stsp return commit;
263 a440fac0 2018-09-06 stsp }
264 a440fac0 2018-09-06 stsp
265 a440fac0 2018-09-06 stsp const struct got_error *
266 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
267 a440fac0 2018-09-06 stsp const char *id_str)
268 a440fac0 2018-09-06 stsp {
269 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
270 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
271 a440fac0 2018-09-06 stsp
272 5df4932d 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
273 5df4932d 2018-11-05 stsp if (err)
274 7762fe12 2018-11-05 stsp return err;
275 a440fac0 2018-09-06 stsp
276 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
277 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
278 a440fac0 2018-09-06 stsp free(qid->id);
279 a440fac0 2018-09-06 stsp free(qid);
280 a440fac0 2018-09-06 stsp return err;
281 a440fac0 2018-09-06 stsp }
282 a440fac0 2018-09-06 stsp
283 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
284 a440fac0 2018-09-06 stsp commit->nparents++;
285 a440fac0 2018-09-06 stsp
286 a440fac0 2018-09-06 stsp return NULL;
287 a440fac0 2018-09-06 stsp }
288 a440fac0 2018-09-06 stsp
289 a440fac0 2018-09-06 stsp static const struct got_error *
290 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
291 a440fac0 2018-09-06 stsp {
292 a440fac0 2018-09-06 stsp int sign = 1;
293 a440fac0 2018-09-06 stsp const char *p = tzstr;
294 a440fac0 2018-09-06 stsp time_t h, m;
295 a440fac0 2018-09-06 stsp
296 a440fac0 2018-09-06 stsp *gmtoff = 0;
297 a440fac0 2018-09-06 stsp
298 a440fac0 2018-09-06 stsp if (*p == '-')
299 a440fac0 2018-09-06 stsp sign = -1;
300 a440fac0 2018-09-06 stsp else if (*p != '+')
301 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
302 a440fac0 2018-09-06 stsp p++;
303 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
304 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
305 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
306 a440fac0 2018-09-06 stsp
307 a440fac0 2018-09-06 stsp p += 2;
308 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
309 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
310 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
311 a440fac0 2018-09-06 stsp
312 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
313 a440fac0 2018-09-06 stsp return NULL;
314 a440fac0 2018-09-06 stsp }
315 a440fac0 2018-09-06 stsp
316 a440fac0 2018-09-06 stsp static const struct got_error *
317 ccb26ccd 2018-11-05 stsp parse_commit_time(time_t *time, time_t *gmtoff, char *committer)
318 a440fac0 2018-09-06 stsp {
319 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
320 a440fac0 2018-09-06 stsp const char *errstr;
321 a440fac0 2018-09-06 stsp char *space, *tzstr;
322 a440fac0 2018-09-06 stsp
323 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
324 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
325 a440fac0 2018-09-06 stsp if (space == NULL)
326 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
327 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
328 a440fac0 2018-09-06 stsp if (tzstr == NULL)
329 638f9024 2019-05-13 stsp return got_error_from_errno("strdup");
330 ccb26ccd 2018-11-05 stsp err = parse_gmtoff(gmtoff, tzstr);
331 a440fac0 2018-09-06 stsp free(tzstr);
332 a440fac0 2018-09-06 stsp if (err)
333 a440fac0 2018-09-06 stsp return err;
334 a440fac0 2018-09-06 stsp *space = '\0';
335 a440fac0 2018-09-06 stsp
336 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
337 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
338 a440fac0 2018-09-06 stsp if (space == NULL)
339 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
340 a440fac0 2018-09-06 stsp
341 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
342 ccb26ccd 2018-11-05 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
343 a440fac0 2018-09-06 stsp if (errstr)
344 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
345 a440fac0 2018-09-06 stsp
346 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
347 ccb26ccd 2018-11-05 stsp *time -= *gmtoff;
348 a440fac0 2018-09-06 stsp
349 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
350 a440fac0 2018-09-06 stsp *space = '\0';
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 03fa71c8 2018-09-06 stsp void
356 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
357 03fa71c8 2018-09-06 stsp {
358 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
359 03fa71c8 2018-09-06 stsp commit->refcnt--;
360 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
361 03fa71c8 2018-09-06 stsp return;
362 03fa71c8 2018-09-06 stsp }
363 03fa71c8 2018-09-06 stsp
364 dd88155e 2019-06-29 stsp got_object_id_queue_free(&commit->parent_ids);
365 03fa71c8 2018-09-06 stsp free(commit->tree_id);
366 03fa71c8 2018-09-06 stsp free(commit->author);
367 03fa71c8 2018-09-06 stsp free(commit->committer);
368 03fa71c8 2018-09-06 stsp free(commit->logmsg);
369 03fa71c8 2018-09-06 stsp free(commit);
370 45d799e2 2018-12-23 stsp }
371 45d799e2 2018-12-23 stsp
372 45d799e2 2018-12-23 stsp struct got_object_id *
373 45d799e2 2018-12-23 stsp got_object_commit_get_tree_id(struct got_commit_object *commit)
374 45d799e2 2018-12-23 stsp {
375 45d799e2 2018-12-23 stsp return commit->tree_id;
376 45d799e2 2018-12-23 stsp }
377 45d799e2 2018-12-23 stsp
378 45d799e2 2018-12-23 stsp int
379 45d799e2 2018-12-23 stsp got_object_commit_get_nparents(struct got_commit_object *commit)
380 45d799e2 2018-12-23 stsp {
381 45d799e2 2018-12-23 stsp return commit->nparents;
382 03fa71c8 2018-09-06 stsp }
383 03fa71c8 2018-09-06 stsp
384 45d799e2 2018-12-23 stsp const struct got_object_id_queue *
385 45d799e2 2018-12-23 stsp got_object_commit_get_parent_ids(struct got_commit_object *commit)
386 45d799e2 2018-12-23 stsp {
387 45d799e2 2018-12-23 stsp return &commit->parent_ids;
388 45d799e2 2018-12-23 stsp }
389 45d799e2 2018-12-23 stsp
390 45d799e2 2018-12-23 stsp const char *
391 45d799e2 2018-12-23 stsp got_object_commit_get_author(struct got_commit_object *commit)
392 45d799e2 2018-12-23 stsp {
393 45d799e2 2018-12-23 stsp return commit->author;
394 45d799e2 2018-12-23 stsp }
395 45d799e2 2018-12-23 stsp
396 45d799e2 2018-12-23 stsp time_t
397 45d799e2 2018-12-23 stsp got_object_commit_get_author_time(struct got_commit_object *commit)
398 45d799e2 2018-12-23 stsp {
399 45d799e2 2018-12-23 stsp return commit->author_time;
400 45d799e2 2018-12-23 stsp }
401 45d799e2 2018-12-23 stsp
402 45d799e2 2018-12-23 stsp time_t got_object_commit_get_author_gmtoff(struct got_commit_object *commit)
403 45d799e2 2018-12-23 stsp {
404 45d799e2 2018-12-23 stsp return commit->author_gmtoff;
405 45d799e2 2018-12-23 stsp }
406 45d799e2 2018-12-23 stsp
407 45d799e2 2018-12-23 stsp const char *
408 45d799e2 2018-12-23 stsp got_object_commit_get_committer(struct got_commit_object *commit)
409 45d799e2 2018-12-23 stsp {
410 45d799e2 2018-12-23 stsp return commit->committer;
411 45d799e2 2018-12-23 stsp }
412 45d799e2 2018-12-23 stsp
413 45d799e2 2018-12-23 stsp time_t
414 45d799e2 2018-12-23 stsp got_object_commit_get_committer_time(struct got_commit_object *commit)
415 45d799e2 2018-12-23 stsp {
416 45d799e2 2018-12-23 stsp return commit->committer_time;
417 45d799e2 2018-12-23 stsp }
418 45d799e2 2018-12-23 stsp
419 45d799e2 2018-12-23 stsp time_t
420 45d799e2 2018-12-23 stsp got_object_commit_get_committer_gmtoff(struct got_commit_object *commit)
421 45d799e2 2018-12-23 stsp {
422 45d799e2 2018-12-23 stsp return commit->committer_gmtoff;
423 45d799e2 2018-12-23 stsp }
424 45d799e2 2018-12-23 stsp
425 45d799e2 2018-12-23 stsp const char *
426 45d799e2 2018-12-23 stsp got_object_commit_get_logmsg(struct got_commit_object *commit)
427 45d799e2 2018-12-23 stsp {
428 45d799e2 2018-12-23 stsp return commit->logmsg;
429 45d799e2 2018-12-23 stsp }
430 45d799e2 2018-12-23 stsp
431 a440fac0 2018-09-06 stsp const struct got_error *
432 5e0b25c4 2018-12-24 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
433 5e0b25c4 2018-12-24 stsp size_t len)
434 a440fac0 2018-09-06 stsp {
435 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
436 a440fac0 2018-09-06 stsp char *s = buf;
437 ff2a4428 2019-03-19 stsp size_t label_len;
438 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
439 230a42bd 2019-05-11 jcs
440 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
441 a440fac0 2018-09-06 stsp if (*commit == NULL)
442 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_commit_alloc_partial");
443 a440fac0 2018-09-06 stsp
444 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_TREE);
445 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_TREE, label_len) == 0) {
446 ff2a4428 2019-03-19 stsp remain -= label_len;
447 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
448 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
449 a440fac0 2018-09-06 stsp goto done;
450 a440fac0 2018-09-06 stsp }
451 ff2a4428 2019-03-19 stsp s += label_len;
452 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
453 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
454 a440fac0 2018-09-06 stsp goto done;
455 a440fac0 2018-09-06 stsp }
456 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
457 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
458 a440fac0 2018-09-06 stsp } else {
459 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
460 a440fac0 2018-09-06 stsp goto done;
461 a440fac0 2018-09-06 stsp }
462 a440fac0 2018-09-06 stsp
463 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_PARENT);
464 ff2a4428 2019-03-19 stsp while (strncmp(s, GOT_COMMIT_LABEL_PARENT, label_len) == 0) {
465 ff2a4428 2019-03-19 stsp remain -= label_len;
466 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
467 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
468 a440fac0 2018-09-06 stsp goto done;
469 a440fac0 2018-09-06 stsp }
470 ff2a4428 2019-03-19 stsp s += label_len;
471 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
472 a440fac0 2018-09-06 stsp if (err)
473 a440fac0 2018-09-06 stsp goto done;
474 a440fac0 2018-09-06 stsp
475 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
476 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
477 a440fac0 2018-09-06 stsp }
478 a440fac0 2018-09-06 stsp
479 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_AUTHOR);
480 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_AUTHOR, label_len) == 0) {
481 a440fac0 2018-09-06 stsp char *p;
482 a440fac0 2018-09-06 stsp size_t slen;
483 a440fac0 2018-09-06 stsp
484 ff2a4428 2019-03-19 stsp remain -= label_len;
485 a440fac0 2018-09-06 stsp if (remain <= 0) {
486 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 a440fac0 2018-09-06 stsp goto done;
488 a440fac0 2018-09-06 stsp }
489 ff2a4428 2019-03-19 stsp s += label_len;
490 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
491 a440fac0 2018-09-06 stsp if (p == NULL) {
492 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
493 a440fac0 2018-09-06 stsp goto done;
494 a440fac0 2018-09-06 stsp }
495 a440fac0 2018-09-06 stsp *p = '\0';
496 a440fac0 2018-09-06 stsp slen = strlen(s);
497 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->author_time,
498 ccb26ccd 2018-11-05 stsp &(*commit)->author_gmtoff, s);
499 a440fac0 2018-09-06 stsp if (err)
500 a440fac0 2018-09-06 stsp goto done;
501 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
502 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
503 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
504 a440fac0 2018-09-06 stsp goto done;
505 a440fac0 2018-09-06 stsp }
506 a440fac0 2018-09-06 stsp s += slen + 1;
507 a440fac0 2018-09-06 stsp remain -= slen + 1;
508 a440fac0 2018-09-06 stsp }
509 a440fac0 2018-09-06 stsp
510 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_COMMIT_LABEL_COMMITTER);
511 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_COMMIT_LABEL_COMMITTER, label_len) == 0) {
512 a440fac0 2018-09-06 stsp char *p;
513 a440fac0 2018-09-06 stsp size_t slen;
514 a440fac0 2018-09-06 stsp
515 ff2a4428 2019-03-19 stsp remain -= label_len;
516 a440fac0 2018-09-06 stsp if (remain <= 0) {
517 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
518 a440fac0 2018-09-06 stsp goto done;
519 a440fac0 2018-09-06 stsp }
520 ff2a4428 2019-03-19 stsp s += label_len;
521 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
522 a440fac0 2018-09-06 stsp if (p == NULL) {
523 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
524 a440fac0 2018-09-06 stsp goto done;
525 a440fac0 2018-09-06 stsp }
526 a440fac0 2018-09-06 stsp *p = '\0';
527 a440fac0 2018-09-06 stsp slen = strlen(s);
528 ccb26ccd 2018-11-05 stsp err = parse_commit_time(&(*commit)->committer_time,
529 ccb26ccd 2018-11-05 stsp &(*commit)->committer_gmtoff, s);
530 a440fac0 2018-09-06 stsp if (err)
531 a440fac0 2018-09-06 stsp goto done;
532 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
533 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
534 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
535 a440fac0 2018-09-06 stsp goto done;
536 a440fac0 2018-09-06 stsp }
537 a440fac0 2018-09-06 stsp s += slen + 1;
538 a440fac0 2018-09-06 stsp remain -= slen + 1;
539 a440fac0 2018-09-06 stsp }
540 a440fac0 2018-09-06 stsp
541 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
542 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
543 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
544 a440fac0 2018-09-06 stsp goto done;
545 a440fac0 2018-09-06 stsp }
546 a440fac0 2018-09-06 stsp done:
547 a440fac0 2018-09-06 stsp if (err) {
548 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
549 a440fac0 2018-09-06 stsp *commit = NULL;
550 a440fac0 2018-09-06 stsp }
551 a440fac0 2018-09-06 stsp return err;
552 a440fac0 2018-09-06 stsp }
553 a440fac0 2018-09-06 stsp
554 ad242220 2018-09-08 stsp void
555 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
556 a440fac0 2018-09-06 stsp {
557 a440fac0 2018-09-06 stsp free(te->id);
558 a440fac0 2018-09-06 stsp free(te->name);
559 a440fac0 2018-09-06 stsp free(te);
560 a440fac0 2018-09-06 stsp }
561 a440fac0 2018-09-06 stsp
562 03fa71c8 2018-09-06 stsp void
563 ed175427 2019-05-09 stsp got_object_tree_entries_close(struct got_tree_entries *entries)
564 03fa71c8 2018-09-06 stsp {
565 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
566 03fa71c8 2018-09-06 stsp
567 ed175427 2019-05-09 stsp while (!SIMPLEQ_EMPTY(&entries->head)) {
568 ed175427 2019-05-09 stsp te = SIMPLEQ_FIRST(&entries->head);
569 ed175427 2019-05-09 stsp SIMPLEQ_REMOVE_HEAD(&entries->head, entry);
570 ed175427 2019-05-09 stsp got_object_tree_entry_close(te);
571 ed175427 2019-05-09 stsp }
572 ed175427 2019-05-09 stsp }
573 ed175427 2019-05-09 stsp
574 ed175427 2019-05-09 stsp void
575 ed175427 2019-05-09 stsp got_object_tree_close(struct got_tree_object *tree)
576 ed175427 2019-05-09 stsp {
577 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
578 03fa71c8 2018-09-06 stsp tree->refcnt--;
579 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
580 03fa71c8 2018-09-06 stsp return;
581 03fa71c8 2018-09-06 stsp }
582 03fa71c8 2018-09-06 stsp
583 ed175427 2019-05-09 stsp got_object_tree_entries_close(&tree->entries);
584 03fa71c8 2018-09-06 stsp free(tree);
585 03fa71c8 2018-09-06 stsp }
586 03fa71c8 2018-09-06 stsp
587 a440fac0 2018-09-06 stsp struct got_tree_entry *
588 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
589 a440fac0 2018-09-06 stsp {
590 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
591 a440fac0 2018-09-06 stsp
592 32ac459c 2018-11-05 stsp te = malloc(sizeof(*te));
593 a440fac0 2018-09-06 stsp if (te == NULL)
594 a440fac0 2018-09-06 stsp return NULL;
595 a440fac0 2018-09-06 stsp
596 32ac459c 2018-11-05 stsp te->id = malloc(sizeof(*te->id));
597 a440fac0 2018-09-06 stsp if (te->id == NULL) {
598 a440fac0 2018-09-06 stsp free(te);
599 a440fac0 2018-09-06 stsp te = NULL;
600 a440fac0 2018-09-06 stsp }
601 a440fac0 2018-09-06 stsp return te;
602 a440fac0 2018-09-06 stsp }
603 a440fac0 2018-09-06 stsp
604 a440fac0 2018-09-06 stsp static const struct got_error *
605 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
606 a440fac0 2018-09-06 stsp size_t maxlen)
607 a440fac0 2018-09-06 stsp {
608 8914529d 2019-04-13 stsp char *p, *space;
609 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
610 a440fac0 2018-09-06 stsp
611 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
612 a440fac0 2018-09-06 stsp if (*te == NULL)
613 638f9024 2019-05-13 stsp return got_error_from_errno("got_alloc_tree_entry_partial");
614 a440fac0 2018-09-06 stsp
615 9ef4ac16 2019-04-13 stsp *elen = strnlen(buf, maxlen) + 1;
616 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
617 a440fac0 2018-09-06 stsp free(*te);
618 a440fac0 2018-09-06 stsp *te = NULL;
619 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
620 a440fac0 2018-09-06 stsp }
621 a440fac0 2018-09-06 stsp
622 dedbbd9d 2019-04-13 stsp space = memchr(buf, ' ', *elen);
623 8914529d 2019-04-13 stsp if (space == NULL || space <= buf) {
624 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
625 a440fac0 2018-09-06 stsp free(*te);
626 a440fac0 2018-09-06 stsp *te = NULL;
627 a440fac0 2018-09-06 stsp return err;
628 a440fac0 2018-09-06 stsp }
629 6dfaee02 2018-11-05 stsp (*te)->mode = 0;
630 8914529d 2019-04-13 stsp p = buf;
631 8914529d 2019-04-13 stsp while (p < space) {
632 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
633 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 a440fac0 2018-09-06 stsp goto done;
635 a440fac0 2018-09-06 stsp }
636 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
637 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
638 a440fac0 2018-09-06 stsp p++;
639 a440fac0 2018-09-06 stsp }
640 a440fac0 2018-09-06 stsp
641 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
642 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
643 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
644 a440fac0 2018-09-06 stsp goto done;
645 a440fac0 2018-09-06 stsp }
646 68bf1b1e 2018-11-07 stsp buf += *elen;
647 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
648 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
649 a440fac0 2018-09-06 stsp done:
650 a440fac0 2018-09-06 stsp if (err) {
651 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
652 a440fac0 2018-09-06 stsp *te = NULL;
653 a440fac0 2018-09-06 stsp }
654 a440fac0 2018-09-06 stsp return err;
655 a440fac0 2018-09-06 stsp }
656 a440fac0 2018-09-06 stsp
657 a440fac0 2018-09-06 stsp const struct got_error *
658 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
659 a440fac0 2018-09-06 stsp {
660 a440fac0 2018-09-06 stsp const struct got_error *err;
661 a440fac0 2018-09-06 stsp size_t remain = len;
662 f5d3d7af 2019-02-05 stsp struct got_pathlist_head pathlist;
663 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *pe;
664 f5d3d7af 2019-02-05 stsp
665 f5d3d7af 2019-02-05 stsp TAILQ_INIT(&pathlist);
666 a440fac0 2018-09-06 stsp
667 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
668 a440fac0 2018-09-06 stsp if (*tree == NULL)
669 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
670 a440fac0 2018-09-06 stsp
671 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
672 a440fac0 2018-09-06 stsp
673 a440fac0 2018-09-06 stsp while (remain > 0) {
674 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
675 f5d3d7af 2019-02-05 stsp struct got_pathlist_entry *new = NULL;
676 a440fac0 2018-09-06 stsp size_t elen;
677 a440fac0 2018-09-06 stsp
678 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
679 a440fac0 2018-09-06 stsp if (err)
680 f5d3d7af 2019-02-05 stsp goto done;
681 f5d3d7af 2019-02-05 stsp err = got_pathlist_insert(&new, &pathlist, te->name, te);
682 f5d3d7af 2019-02-05 stsp if (err)
683 f5d3d7af 2019-02-05 stsp goto done;
684 f5d3d7af 2019-02-05 stsp if (new == NULL) {
685 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_TREE_DUP_ENTRY);
686 f5d3d7af 2019-02-05 stsp goto done;
687 f5d3d7af 2019-02-05 stsp }
688 a440fac0 2018-09-06 stsp buf += elen;
689 a440fac0 2018-09-06 stsp remain -= elen;
690 a440fac0 2018-09-06 stsp }
691 a440fac0 2018-09-06 stsp
692 a440fac0 2018-09-06 stsp if (remain != 0) {
693 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
694 13f977b4 2018-11-17 stsp *tree = NULL;
695 f5d3d7af 2019-02-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
696 f5d3d7af 2019-02-05 stsp goto done;
697 a440fac0 2018-09-06 stsp }
698 a440fac0 2018-09-06 stsp
699 f5d3d7af 2019-02-05 stsp TAILQ_FOREACH(pe, &pathlist, entry) {
700 f5d3d7af 2019-02-05 stsp struct got_tree_entry *te = pe->data;
701 f5d3d7af 2019-02-05 stsp (*tree)->entries.nentries++;
702 f5d3d7af 2019-02-05 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
703 f5d3d7af 2019-02-05 stsp }
704 f5d3d7af 2019-02-05 stsp done:
705 f5d3d7af 2019-02-05 stsp got_pathlist_free(&pathlist);
706 f5d3d7af 2019-02-05 stsp return err;
707 a440fac0 2018-09-06 stsp }
708 a440fac0 2018-09-06 stsp
709 f4a881ce 2018-11-17 stsp void
710 f4a881ce 2018-11-17 stsp got_object_tag_close(struct got_tag_object *tag)
711 f4a881ce 2018-11-17 stsp {
712 f4a881ce 2018-11-17 stsp free(tag->tag);
713 f4a881ce 2018-11-17 stsp free(tag->tagger);
714 f4a881ce 2018-11-17 stsp free(tag->tagmsg);
715 f4a881ce 2018-11-17 stsp free(tag);
716 f4a881ce 2018-11-17 stsp }
717 f4a881ce 2018-11-17 stsp
718 ad242220 2018-09-08 stsp const struct got_error *
719 f4a881ce 2018-11-17 stsp got_object_parse_tag(struct got_tag_object **tag, uint8_t *buf, size_t len)
720 f4a881ce 2018-11-17 stsp {
721 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
722 f4a881ce 2018-11-17 stsp size_t remain = len;
723 f4a881ce 2018-11-17 stsp char *s = buf;
724 ff2a4428 2019-03-19 stsp size_t label_len;
725 f4a881ce 2018-11-17 stsp
726 f4a881ce 2018-11-17 stsp *tag = calloc(1, sizeof(**tag));
727 f4a881ce 2018-11-17 stsp if (*tag == NULL)
728 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
729 f4a881ce 2018-11-17 stsp
730 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_OBJECT);
731 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_OBJECT, label_len) == 0) {
732 ff2a4428 2019-03-19 stsp remain -= label_len;
733 f4a881ce 2018-11-17 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
734 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
735 f4a881ce 2018-11-17 stsp goto done;
736 f4a881ce 2018-11-17 stsp }
737 ff2a4428 2019-03-19 stsp s += label_len;
738 f4a881ce 2018-11-17 stsp if (!got_parse_sha1_digest((*tag)->id.sha1, s)) {
739 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
740 f4a881ce 2018-11-17 stsp goto done;
741 f4a881ce 2018-11-17 stsp }
742 f4a881ce 2018-11-17 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
743 f4a881ce 2018-11-17 stsp s += SHA1_DIGEST_STRING_LENGTH;
744 f4a881ce 2018-11-17 stsp } else {
745 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
746 f4a881ce 2018-11-17 stsp goto done;
747 f4a881ce 2018-11-17 stsp }
748 f4a881ce 2018-11-17 stsp
749 f4a881ce 2018-11-17 stsp if (remain <= 0) {
750 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
751 f4a881ce 2018-11-17 stsp goto done;
752 f4a881ce 2018-11-17 stsp }
753 f4a881ce 2018-11-17 stsp
754 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TYPE);
755 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TYPE, label_len) == 0) {
756 ff2a4428 2019-03-19 stsp remain -= label_len;
757 f4a881ce 2018-11-17 stsp if (remain <= 0) {
758 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
759 f4a881ce 2018-11-17 stsp goto done;
760 f4a881ce 2018-11-17 stsp }
761 ff2a4428 2019-03-19 stsp s += label_len;
762 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_OBJ_LABEL_COMMIT,
763 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_COMMIT)) == 0) {
764 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_COMMIT;
765 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_COMMIT);
766 ff2a4428 2019-03-19 stsp s += label_len;
767 ff2a4428 2019-03-19 stsp remain -= label_len;
768 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TREE,
769 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TREE)) == 0) {
770 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TREE;
771 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TREE);
772 ff2a4428 2019-03-19 stsp s += label_len;
773 ff2a4428 2019-03-19 stsp remain -= label_len;
774 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_BLOB,
775 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_BLOB)) == 0) {
776 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_BLOB;
777 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_BLOB);
778 ff2a4428 2019-03-19 stsp s += label_len;
779 ff2a4428 2019-03-19 stsp remain -= label_len;
780 ff2a4428 2019-03-19 stsp } else if (strncmp(s, GOT_OBJ_LABEL_TAG,
781 ff2a4428 2019-03-19 stsp strlen(GOT_OBJ_LABEL_TAG)) == 0) {
782 f4a881ce 2018-11-17 stsp (*tag)->obj_type = GOT_OBJ_TYPE_TAG;
783 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_OBJ_LABEL_TAG);
784 ff2a4428 2019-03-19 stsp s += label_len;
785 ff2a4428 2019-03-19 stsp remain -= label_len;
786 f4a881ce 2018-11-17 stsp } else {
787 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
788 f4a881ce 2018-11-17 stsp goto done;
789 f4a881ce 2018-11-17 stsp }
790 f4a881ce 2018-11-17 stsp
791 f4a881ce 2018-11-17 stsp if (remain <= 0 || *s != '\n') {
792 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
793 f4a881ce 2018-11-17 stsp goto done;
794 f4a881ce 2018-11-17 stsp }
795 f4a881ce 2018-11-17 stsp s++;
796 f4a881ce 2018-11-17 stsp remain--;
797 f4a881ce 2018-11-17 stsp if (remain <= 0) {
798 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
799 f4a881ce 2018-11-17 stsp goto done;
800 f4a881ce 2018-11-17 stsp }
801 f4a881ce 2018-11-17 stsp } else {
802 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
803 f4a881ce 2018-11-17 stsp goto done;
804 f4a881ce 2018-11-17 stsp }
805 f4a881ce 2018-11-17 stsp
806 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAG);
807 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAG, label_len) == 0) {
808 f4a881ce 2018-11-17 stsp char *p;
809 f4a881ce 2018-11-17 stsp size_t slen;
810 ff2a4428 2019-03-19 stsp remain -= label_len;
811 f4a881ce 2018-11-17 stsp if (remain <= 0) {
812 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
813 f4a881ce 2018-11-17 stsp goto done;
814 f4a881ce 2018-11-17 stsp }
815 ff2a4428 2019-03-19 stsp s += label_len;
816 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
817 f4a881ce 2018-11-17 stsp if (p == NULL) {
818 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
819 f4a881ce 2018-11-17 stsp goto done;
820 f4a881ce 2018-11-17 stsp }
821 f4a881ce 2018-11-17 stsp *p = '\0';
822 f4a881ce 2018-11-17 stsp slen = strlen(s);
823 f4a881ce 2018-11-17 stsp (*tag)->tag = strndup(s, slen);
824 f4a881ce 2018-11-17 stsp if ((*tag)->tag == NULL) {
825 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
826 f4a881ce 2018-11-17 stsp goto done;
827 f4a881ce 2018-11-17 stsp }
828 f4a881ce 2018-11-17 stsp s += slen + 1;
829 f4a881ce 2018-11-17 stsp remain -= slen + 1;
830 f4a881ce 2018-11-17 stsp if (remain <= 0) {
831 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
832 f4a881ce 2018-11-17 stsp goto done;
833 f4a881ce 2018-11-17 stsp }
834 f4a881ce 2018-11-17 stsp } else {
835 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
836 f4a881ce 2018-11-17 stsp goto done;
837 f4a881ce 2018-11-17 stsp }
838 f4a881ce 2018-11-17 stsp
839 ff2a4428 2019-03-19 stsp label_len = strlen(GOT_TAG_LABEL_TAGGER);
840 ff2a4428 2019-03-19 stsp if (strncmp(s, GOT_TAG_LABEL_TAGGER, label_len) == 0) {
841 f4a881ce 2018-11-17 stsp char *p;
842 f4a881ce 2018-11-17 stsp size_t slen;
843 f4a881ce 2018-11-17 stsp
844 ff2a4428 2019-03-19 stsp remain -= label_len;
845 f4a881ce 2018-11-17 stsp if (remain <= 0) {
846 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
847 f4a881ce 2018-11-17 stsp goto done;
848 f4a881ce 2018-11-17 stsp }
849 ff2a4428 2019-03-19 stsp s += label_len;
850 dedbbd9d 2019-04-13 stsp p = memchr(s, '\n', remain);
851 f4a881ce 2018-11-17 stsp if (p == NULL) {
852 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
853 f4a881ce 2018-11-17 stsp goto done;
854 f4a881ce 2018-11-17 stsp }
855 f4a881ce 2018-11-17 stsp *p = '\0';
856 f4a881ce 2018-11-17 stsp slen = strlen(s);
857 f4a881ce 2018-11-17 stsp err = parse_commit_time(&(*tag)->tagger_time,
858 f4a881ce 2018-11-17 stsp &(*tag)->tagger_gmtoff, s);
859 f4a881ce 2018-11-17 stsp if (err)
860 f4a881ce 2018-11-17 stsp goto done;
861 f4a881ce 2018-11-17 stsp (*tag)->tagger = strdup(s);
862 f4a881ce 2018-11-17 stsp if ((*tag)->tagger == NULL) {
863 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
864 f4a881ce 2018-11-17 stsp goto done;
865 f4a881ce 2018-11-17 stsp }
866 f4a881ce 2018-11-17 stsp s += slen + 1;
867 f4a881ce 2018-11-17 stsp remain -= slen + 1;
868 f4a881ce 2018-11-17 stsp if (remain <= 0) {
869 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
870 f4a881ce 2018-11-17 stsp goto done;
871 f4a881ce 2018-11-17 stsp }
872 f4a881ce 2018-11-17 stsp } else {
873 e0e55b50 2019-02-01 stsp /* Some old tags in the Linux git repo have no tagger. */
874 e0e55b50 2019-02-01 stsp (*tag)->tagger = strdup("");
875 e0e55b50 2019-02-01 stsp if ((*tag)->tagger == NULL) {
876 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
877 e0e55b50 2019-02-01 stsp goto done;
878 e0e55b50 2019-02-01 stsp }
879 f4a881ce 2018-11-17 stsp }
880 f4a881ce 2018-11-17 stsp
881 f4a881ce 2018-11-17 stsp (*tag)->tagmsg = strndup(s, remain);
882 f4a881ce 2018-11-17 stsp if ((*tag)->tagmsg == NULL) {
883 638f9024 2019-05-13 stsp err = got_error_from_errno("strndup");
884 f4a881ce 2018-11-17 stsp goto done;
885 f4a881ce 2018-11-17 stsp }
886 f4a881ce 2018-11-17 stsp done:
887 f4a881ce 2018-11-17 stsp if (err) {
888 f4a881ce 2018-11-17 stsp got_object_tag_close(*tag);
889 f4a881ce 2018-11-17 stsp *tag = NULL;
890 f4a881ce 2018-11-17 stsp }
891 f4a881ce 2018-11-17 stsp return err;
892 f4a881ce 2018-11-17 stsp }
893 f4a881ce 2018-11-17 stsp
894 f4a881ce 2018-11-17 stsp const struct got_error *
895 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
896 a440fac0 2018-09-06 stsp {
897 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
898 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
899 a440fac0 2018-09-06 stsp size_t n, total, remain;
900 a440fac0 2018-09-06 stsp uint8_t *buf;
901 a440fac0 2018-09-06 stsp
902 a440fac0 2018-09-06 stsp *outbuf = NULL;
903 a440fac0 2018-09-06 stsp *outlen = 0;
904 a440fac0 2018-09-06 stsp
905 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
906 a440fac0 2018-09-06 stsp if (buf == NULL)
907 638f9024 2019-05-13 stsp return got_error_from_errno("malloc");
908 a440fac0 2018-09-06 stsp
909 a440fac0 2018-09-06 stsp remain = blocksize;
910 a440fac0 2018-09-06 stsp total = 0;
911 656b1f76 2019-05-11 jcs for (;;) {
912 a440fac0 2018-09-06 stsp if (remain == 0) {
913 a440fac0 2018-09-06 stsp uint8_t *newbuf;
914 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
915 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
916 638f9024 2019-05-13 stsp err = got_error_from_errno("reallocarray");
917 a440fac0 2018-09-06 stsp goto done;
918 a440fac0 2018-09-06 stsp }
919 a440fac0 2018-09-06 stsp buf = newbuf;
920 a440fac0 2018-09-06 stsp remain += blocksize;
921 a440fac0 2018-09-06 stsp }
922 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
923 a440fac0 2018-09-06 stsp if (n == 0) {
924 a440fac0 2018-09-06 stsp if (ferror(f)) {
925 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
926 a440fac0 2018-09-06 stsp goto done;
927 a440fac0 2018-09-06 stsp }
928 a440fac0 2018-09-06 stsp break; /* EOF */
929 a440fac0 2018-09-06 stsp }
930 a440fac0 2018-09-06 stsp remain -= n;
931 a440fac0 2018-09-06 stsp total += n;
932 a440fac0 2018-09-06 stsp };
933 a440fac0 2018-09-06 stsp
934 a440fac0 2018-09-06 stsp done:
935 a440fac0 2018-09-06 stsp if (err == NULL) {
936 a440fac0 2018-09-06 stsp *outbuf = buf;
937 a440fac0 2018-09-06 stsp *outlen = total;
938 a440fac0 2018-09-06 stsp } else
939 a440fac0 2018-09-06 stsp free(buf);
940 ad242220 2018-09-08 stsp return err;
941 a440fac0 2018-09-06 stsp }