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 ad242220 2018-09-08 stsp #include "got_lib_privsep.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_pack.h"
47 a440fac0 2018-09-06 stsp #include "got_lib_inflate.h"
48 a440fac0 2018-09-06 stsp #include "got_lib_object.h"
49 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.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 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_TREE "tree"
58 a440fac0 2018-09-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
59 a440fac0 2018-09-06 stsp
60 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
61 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
62 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
63 a440fac0 2018-09-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
64 2ff12563 2018-09-15 stsp
65 2ff12563 2018-09-15 stsp const struct got_error *
66 2ff12563 2018-09-15 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
67 2ff12563 2018-09-15 stsp {
68 2ff12563 2018-09-15 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
69 2ff12563 2018-09-15 stsp
70 2ff12563 2018-09-15 stsp *outbuf = malloc(len);
71 2ff12563 2018-09-15 stsp if (*outbuf == NULL)
72 2ff12563 2018-09-15 stsp return got_error_from_errno();
73 2ff12563 2018-09-15 stsp
74 2ff12563 2018-09-15 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 2ff12563 2018-09-15 stsp free(*outbuf);
76 2ff12563 2018-09-15 stsp *outbuf = NULL;
77 2ff12563 2018-09-15 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
78 2ff12563 2018-09-15 stsp }
79 a440fac0 2018-09-06 stsp
80 2ff12563 2018-09-15 stsp return NULL;
81 2ff12563 2018-09-15 stsp }
82 2ff12563 2018-09-15 stsp
83 03fa71c8 2018-09-06 stsp void
84 03fa71c8 2018-09-06 stsp got_object_close(struct got_object *obj)
85 03fa71c8 2018-09-06 stsp {
86 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0) {
87 03fa71c8 2018-09-06 stsp obj->refcnt--;
88 03fa71c8 2018-09-06 stsp if (obj->refcnt > 0)
89 03fa71c8 2018-09-06 stsp return;
90 03fa71c8 2018-09-06 stsp }
91 03fa71c8 2018-09-06 stsp
92 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
93 03fa71c8 2018-09-06 stsp struct got_delta *delta;
94 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
95 03fa71c8 2018-09-06 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
96 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
97 03fa71c8 2018-09-06 stsp got_delta_close(delta);
98 03fa71c8 2018-09-06 stsp }
99 03fa71c8 2018-09-06 stsp }
100 03fa71c8 2018-09-06 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
101 03fa71c8 2018-09-06 stsp free(obj->path_packfile);
102 03fa71c8 2018-09-06 stsp free(obj);
103 03fa71c8 2018-09-06 stsp }
104 03fa71c8 2018-09-06 stsp
105 03fa71c8 2018-09-06 stsp void
106 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
107 03fa71c8 2018-09-06 stsp {
108 03fa71c8 2018-09-06 stsp free(qid->id);
109 03fa71c8 2018-09-06 stsp free(qid);
110 876c234b 2018-09-10 stsp }
111 876c234b 2018-09-10 stsp
112 a440fac0 2018-09-06 stsp struct got_commit_object *
113 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
114 a440fac0 2018-09-06 stsp {
115 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
116 a440fac0 2018-09-06 stsp
117 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
118 a440fac0 2018-09-06 stsp if (commit == NULL)
119 a440fac0 2018-09-06 stsp return NULL;
120 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
121 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
122 a440fac0 2018-09-06 stsp free(commit);
123 a440fac0 2018-09-06 stsp return NULL;
124 a440fac0 2018-09-06 stsp }
125 a440fac0 2018-09-06 stsp
126 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
127 a440fac0 2018-09-06 stsp
128 a440fac0 2018-09-06 stsp return commit;
129 a440fac0 2018-09-06 stsp }
130 a440fac0 2018-09-06 stsp
131 a440fac0 2018-09-06 stsp const struct got_error *
132 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
133 a440fac0 2018-09-06 stsp const char *id_str)
134 a440fac0 2018-09-06 stsp {
135 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
136 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
137 a440fac0 2018-09-06 stsp
138 a440fac0 2018-09-06 stsp qid = malloc(sizeof(*qid));
139 a440fac0 2018-09-06 stsp if (qid == NULL)
140 a440fac0 2018-09-06 stsp return got_error_from_errno();
141 a440fac0 2018-09-06 stsp
142 a440fac0 2018-09-06 stsp qid->id = malloc(sizeof(*qid->id));
143 a440fac0 2018-09-06 stsp if (qid->id == NULL) {
144 a440fac0 2018-09-06 stsp err = got_error_from_errno();
145 a440fac0 2018-09-06 stsp got_object_qid_free(qid);
146 a440fac0 2018-09-06 stsp return err;
147 a440fac0 2018-09-06 stsp }
148 a440fac0 2018-09-06 stsp
149 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
150 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
151 a440fac0 2018-09-06 stsp free(qid->id);
152 a440fac0 2018-09-06 stsp free(qid);
153 a440fac0 2018-09-06 stsp return err;
154 a440fac0 2018-09-06 stsp }
155 a440fac0 2018-09-06 stsp
156 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
157 a440fac0 2018-09-06 stsp commit->nparents++;
158 a440fac0 2018-09-06 stsp
159 a440fac0 2018-09-06 stsp return NULL;
160 a440fac0 2018-09-06 stsp }
161 a440fac0 2018-09-06 stsp
162 a440fac0 2018-09-06 stsp static const struct got_error *
163 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
164 a440fac0 2018-09-06 stsp {
165 a440fac0 2018-09-06 stsp int sign = 1;
166 a440fac0 2018-09-06 stsp const char *p = tzstr;
167 a440fac0 2018-09-06 stsp time_t h, m;
168 a440fac0 2018-09-06 stsp
169 a440fac0 2018-09-06 stsp *gmtoff = 0;
170 a440fac0 2018-09-06 stsp
171 a440fac0 2018-09-06 stsp if (*p == '-')
172 a440fac0 2018-09-06 stsp sign = -1;
173 a440fac0 2018-09-06 stsp else if (*p != '+')
174 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
175 a440fac0 2018-09-06 stsp p++;
176 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
177 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
178 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
179 a440fac0 2018-09-06 stsp
180 a440fac0 2018-09-06 stsp p += 2;
181 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
182 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
183 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
184 a440fac0 2018-09-06 stsp
185 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
186 a440fac0 2018-09-06 stsp return NULL;
187 a440fac0 2018-09-06 stsp }
188 a440fac0 2018-09-06 stsp
189 a440fac0 2018-09-06 stsp static const struct got_error *
190 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
191 a440fac0 2018-09-06 stsp {
192 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
193 a440fac0 2018-09-06 stsp const char *errstr;
194 a440fac0 2018-09-06 stsp char *space, *tzstr;
195 a440fac0 2018-09-06 stsp time_t gmtoff;
196 a440fac0 2018-09-06 stsp time_t time;
197 a440fac0 2018-09-06 stsp
198 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
199 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
200 a440fac0 2018-09-06 stsp if (space == NULL)
201 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
202 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
203 a440fac0 2018-09-06 stsp if (tzstr == NULL)
204 a440fac0 2018-09-06 stsp return got_error_from_errno();
205 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
206 a440fac0 2018-09-06 stsp free(tzstr);
207 a440fac0 2018-09-06 stsp if (err)
208 a440fac0 2018-09-06 stsp return err;
209 a440fac0 2018-09-06 stsp *space = '\0';
210 a440fac0 2018-09-06 stsp
211 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
212 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
213 a440fac0 2018-09-06 stsp if (space == NULL)
214 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
215 a440fac0 2018-09-06 stsp
216 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
217 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
218 a440fac0 2018-09-06 stsp if (errstr)
219 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
220 a440fac0 2018-09-06 stsp
221 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
222 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
223 a440fac0 2018-09-06 stsp time -= gmtoff;
224 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
225 a440fac0 2018-09-06 stsp return got_error_from_errno();
226 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
227 a440fac0 2018-09-06 stsp
228 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
229 a440fac0 2018-09-06 stsp *space = '\0';
230 a440fac0 2018-09-06 stsp
231 a440fac0 2018-09-06 stsp return NULL;
232 a440fac0 2018-09-06 stsp }
233 a440fac0 2018-09-06 stsp
234 03fa71c8 2018-09-06 stsp void
235 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
236 03fa71c8 2018-09-06 stsp {
237 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
238 03fa71c8 2018-09-06 stsp
239 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
240 03fa71c8 2018-09-06 stsp commit->refcnt--;
241 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
242 03fa71c8 2018-09-06 stsp return;
243 03fa71c8 2018-09-06 stsp }
244 03fa71c8 2018-09-06 stsp
245 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
246 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
247 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
248 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
249 03fa71c8 2018-09-06 stsp }
250 03fa71c8 2018-09-06 stsp
251 03fa71c8 2018-09-06 stsp free(commit->tree_id);
252 03fa71c8 2018-09-06 stsp free(commit->author);
253 03fa71c8 2018-09-06 stsp free(commit->committer);
254 03fa71c8 2018-09-06 stsp free(commit->logmsg);
255 03fa71c8 2018-09-06 stsp free(commit);
256 03fa71c8 2018-09-06 stsp }
257 03fa71c8 2018-09-06 stsp
258 a440fac0 2018-09-06 stsp const struct got_error *
259 a440fac0 2018-09-06 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf, size_t len)
260 a440fac0 2018-09-06 stsp {
261 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
262 a440fac0 2018-09-06 stsp char *s = buf;
263 a440fac0 2018-09-06 stsp size_t tlen;
264 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
265 a440fac0 2018-09-06 stsp
266 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
267 a440fac0 2018-09-06 stsp if (*commit == NULL)
268 a440fac0 2018-09-06 stsp return got_error_from_errno();
269 a440fac0 2018-09-06 stsp
270 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
271 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
272 a440fac0 2018-09-06 stsp remain -= tlen;
273 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
274 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
275 a440fac0 2018-09-06 stsp goto done;
276 a440fac0 2018-09-06 stsp }
277 a440fac0 2018-09-06 stsp s += tlen;
278 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
279 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
280 a440fac0 2018-09-06 stsp goto done;
281 a440fac0 2018-09-06 stsp }
282 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
283 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
284 a440fac0 2018-09-06 stsp } else {
285 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
286 a440fac0 2018-09-06 stsp goto done;
287 a440fac0 2018-09-06 stsp }
288 a440fac0 2018-09-06 stsp
289 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
290 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
291 a440fac0 2018-09-06 stsp remain -= tlen;
292 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
293 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
294 a440fac0 2018-09-06 stsp goto done;
295 a440fac0 2018-09-06 stsp }
296 a440fac0 2018-09-06 stsp s += tlen;
297 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
298 a440fac0 2018-09-06 stsp if (err)
299 a440fac0 2018-09-06 stsp goto done;
300 a440fac0 2018-09-06 stsp
301 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
302 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
303 a440fac0 2018-09-06 stsp }
304 a440fac0 2018-09-06 stsp
305 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
306 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
307 a440fac0 2018-09-06 stsp char *p;
308 a440fac0 2018-09-06 stsp size_t slen;
309 a440fac0 2018-09-06 stsp
310 a440fac0 2018-09-06 stsp remain -= tlen;
311 a440fac0 2018-09-06 stsp if (remain <= 0) {
312 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
313 a440fac0 2018-09-06 stsp goto done;
314 a440fac0 2018-09-06 stsp }
315 a440fac0 2018-09-06 stsp s += tlen;
316 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
317 a440fac0 2018-09-06 stsp if (p == NULL) {
318 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
319 a440fac0 2018-09-06 stsp goto done;
320 a440fac0 2018-09-06 stsp }
321 a440fac0 2018-09-06 stsp *p = '\0';
322 a440fac0 2018-09-06 stsp slen = strlen(s);
323 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
324 a440fac0 2018-09-06 stsp if (err)
325 a440fac0 2018-09-06 stsp goto done;
326 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
327 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
328 a440fac0 2018-09-06 stsp err = got_error_from_errno();
329 a440fac0 2018-09-06 stsp goto done;
330 a440fac0 2018-09-06 stsp }
331 a440fac0 2018-09-06 stsp s += slen + 1;
332 a440fac0 2018-09-06 stsp remain -= slen + 1;
333 a440fac0 2018-09-06 stsp }
334 a440fac0 2018-09-06 stsp
335 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
336 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
337 a440fac0 2018-09-06 stsp char *p;
338 a440fac0 2018-09-06 stsp size_t slen;
339 a440fac0 2018-09-06 stsp
340 a440fac0 2018-09-06 stsp remain -= tlen;
341 a440fac0 2018-09-06 stsp if (remain <= 0) {
342 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
343 a440fac0 2018-09-06 stsp goto done;
344 a440fac0 2018-09-06 stsp }
345 a440fac0 2018-09-06 stsp s += tlen;
346 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
347 a440fac0 2018-09-06 stsp if (p == NULL) {
348 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
349 a440fac0 2018-09-06 stsp goto done;
350 a440fac0 2018-09-06 stsp }
351 a440fac0 2018-09-06 stsp *p = '\0';
352 a440fac0 2018-09-06 stsp slen = strlen(s);
353 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
354 a440fac0 2018-09-06 stsp if (err)
355 a440fac0 2018-09-06 stsp goto done;
356 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
357 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
358 a440fac0 2018-09-06 stsp err = got_error_from_errno();
359 a440fac0 2018-09-06 stsp goto done;
360 a440fac0 2018-09-06 stsp }
361 a440fac0 2018-09-06 stsp s += slen + 1;
362 a440fac0 2018-09-06 stsp remain -= slen + 1;
363 a440fac0 2018-09-06 stsp }
364 a440fac0 2018-09-06 stsp
365 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
366 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
367 a440fac0 2018-09-06 stsp err = got_error_from_errno();
368 a440fac0 2018-09-06 stsp goto done;
369 a440fac0 2018-09-06 stsp }
370 a440fac0 2018-09-06 stsp done:
371 a440fac0 2018-09-06 stsp if (err) {
372 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
373 a440fac0 2018-09-06 stsp *commit = NULL;
374 a440fac0 2018-09-06 stsp }
375 a440fac0 2018-09-06 stsp return err;
376 a440fac0 2018-09-06 stsp }
377 a440fac0 2018-09-06 stsp
378 ad242220 2018-09-08 stsp void
379 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
380 a440fac0 2018-09-06 stsp {
381 a440fac0 2018-09-06 stsp free(te->id);
382 a440fac0 2018-09-06 stsp free(te->name);
383 a440fac0 2018-09-06 stsp free(te);
384 a440fac0 2018-09-06 stsp }
385 a440fac0 2018-09-06 stsp
386 03fa71c8 2018-09-06 stsp void
387 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
388 03fa71c8 2018-09-06 stsp {
389 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
390 03fa71c8 2018-09-06 stsp
391 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
392 03fa71c8 2018-09-06 stsp tree->refcnt--;
393 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
394 03fa71c8 2018-09-06 stsp return;
395 03fa71c8 2018-09-06 stsp }
396 03fa71c8 2018-09-06 stsp
397 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
398 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
399 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
400 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
401 03fa71c8 2018-09-06 stsp }
402 03fa71c8 2018-09-06 stsp
403 03fa71c8 2018-09-06 stsp free(tree);
404 03fa71c8 2018-09-06 stsp }
405 03fa71c8 2018-09-06 stsp
406 a440fac0 2018-09-06 stsp struct got_tree_entry *
407 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
408 a440fac0 2018-09-06 stsp {
409 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
410 a440fac0 2018-09-06 stsp
411 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
412 a440fac0 2018-09-06 stsp if (te == NULL)
413 a440fac0 2018-09-06 stsp return NULL;
414 a440fac0 2018-09-06 stsp
415 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
416 a440fac0 2018-09-06 stsp if (te->id == NULL) {
417 a440fac0 2018-09-06 stsp free(te);
418 a440fac0 2018-09-06 stsp te = NULL;
419 a440fac0 2018-09-06 stsp }
420 a440fac0 2018-09-06 stsp return te;
421 a440fac0 2018-09-06 stsp }
422 a440fac0 2018-09-06 stsp
423 a440fac0 2018-09-06 stsp static const struct got_error *
424 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
425 a440fac0 2018-09-06 stsp size_t maxlen)
426 a440fac0 2018-09-06 stsp {
427 a440fac0 2018-09-06 stsp char *p = buf, *space;
428 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
429 a440fac0 2018-09-06 stsp
430 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
431 a440fac0 2018-09-06 stsp if (*te == NULL)
432 a440fac0 2018-09-06 stsp return got_error_from_errno();
433 a440fac0 2018-09-06 stsp
434 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
435 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
436 a440fac0 2018-09-06 stsp free(*te);
437 a440fac0 2018-09-06 stsp *te = NULL;
438 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
439 a440fac0 2018-09-06 stsp }
440 a440fac0 2018-09-06 stsp
441 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
442 a440fac0 2018-09-06 stsp if (space == NULL) {
443 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
444 a440fac0 2018-09-06 stsp free(*te);
445 a440fac0 2018-09-06 stsp *te = NULL;
446 a440fac0 2018-09-06 stsp return err;
447 a440fac0 2018-09-06 stsp }
448 a440fac0 2018-09-06 stsp while (*p != ' ') {
449 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
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 (*te)->mode <<= 3;
454 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
455 a440fac0 2018-09-06 stsp p++;
456 a440fac0 2018-09-06 stsp }
457 a440fac0 2018-09-06 stsp
458 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
459 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
460 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
461 a440fac0 2018-09-06 stsp goto done;
462 a440fac0 2018-09-06 stsp }
463 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
464 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
465 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
466 a440fac0 2018-09-06 stsp done:
467 a440fac0 2018-09-06 stsp if (err) {
468 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
469 a440fac0 2018-09-06 stsp *te = NULL;
470 a440fac0 2018-09-06 stsp }
471 a440fac0 2018-09-06 stsp return err;
472 a440fac0 2018-09-06 stsp }
473 a440fac0 2018-09-06 stsp
474 a440fac0 2018-09-06 stsp const struct got_error *
475 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
476 a440fac0 2018-09-06 stsp {
477 a440fac0 2018-09-06 stsp const struct got_error *err;
478 a440fac0 2018-09-06 stsp size_t remain = len;
479 a440fac0 2018-09-06 stsp
480 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
481 a440fac0 2018-09-06 stsp if (*tree == NULL)
482 a440fac0 2018-09-06 stsp return got_error_from_errno();
483 a440fac0 2018-09-06 stsp
484 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
485 a440fac0 2018-09-06 stsp
486 a440fac0 2018-09-06 stsp while (remain > 0) {
487 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
488 a440fac0 2018-09-06 stsp size_t elen;
489 a440fac0 2018-09-06 stsp
490 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
491 a440fac0 2018-09-06 stsp if (err)
492 a440fac0 2018-09-06 stsp return err;
493 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
494 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
495 a440fac0 2018-09-06 stsp buf += elen;
496 a440fac0 2018-09-06 stsp remain -= elen;
497 a440fac0 2018-09-06 stsp }
498 a440fac0 2018-09-06 stsp
499 a440fac0 2018-09-06 stsp if (remain != 0) {
500 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
501 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
502 a440fac0 2018-09-06 stsp }
503 a440fac0 2018-09-06 stsp
504 a440fac0 2018-09-06 stsp return NULL;
505 a440fac0 2018-09-06 stsp }
506 a440fac0 2018-09-06 stsp
507 ad242220 2018-09-08 stsp const struct got_error *
508 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
509 a440fac0 2018-09-06 stsp {
510 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
511 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
512 a440fac0 2018-09-06 stsp size_t n, total, remain;
513 a440fac0 2018-09-06 stsp uint8_t *buf;
514 a440fac0 2018-09-06 stsp
515 a440fac0 2018-09-06 stsp *outbuf = NULL;
516 a440fac0 2018-09-06 stsp *outlen = 0;
517 a440fac0 2018-09-06 stsp
518 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
519 a440fac0 2018-09-06 stsp if (buf == NULL)
520 a440fac0 2018-09-06 stsp return got_error_from_errno();
521 a440fac0 2018-09-06 stsp
522 a440fac0 2018-09-06 stsp remain = blocksize;
523 a440fac0 2018-09-06 stsp total = 0;
524 a440fac0 2018-09-06 stsp while (1) {
525 a440fac0 2018-09-06 stsp if (remain == 0) {
526 a440fac0 2018-09-06 stsp uint8_t *newbuf;
527 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
528 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
529 a440fac0 2018-09-06 stsp err = got_error_from_errno();
530 a440fac0 2018-09-06 stsp goto done;
531 a440fac0 2018-09-06 stsp }
532 a440fac0 2018-09-06 stsp buf = newbuf;
533 a440fac0 2018-09-06 stsp remain += blocksize;
534 a440fac0 2018-09-06 stsp }
535 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
536 a440fac0 2018-09-06 stsp if (n == 0) {
537 a440fac0 2018-09-06 stsp if (ferror(f)) {
538 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
539 a440fac0 2018-09-06 stsp goto done;
540 a440fac0 2018-09-06 stsp }
541 a440fac0 2018-09-06 stsp break; /* EOF */
542 a440fac0 2018-09-06 stsp }
543 a440fac0 2018-09-06 stsp remain -= n;
544 a440fac0 2018-09-06 stsp total += n;
545 a440fac0 2018-09-06 stsp };
546 a440fac0 2018-09-06 stsp
547 a440fac0 2018-09-06 stsp done:
548 a440fac0 2018-09-06 stsp if (err == NULL) {
549 a440fac0 2018-09-06 stsp *outbuf = buf;
550 a440fac0 2018-09-06 stsp *outlen = total;
551 a440fac0 2018-09-06 stsp } else
552 a440fac0 2018-09-06 stsp free(buf);
553 ad242220 2018-09-08 stsp return err;
554 a440fac0 2018-09-06 stsp }