Blame


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