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