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 a440fac0 2018-09-06 stsp #include "got_lib_inflate.h"
46 a440fac0 2018-09-06 stsp #include "got_lib_object.h"
47 7762fe12 2018-11-05 stsp #include "got_lib_privsep.h"
48 7762fe12 2018-11-05 stsp #include "got_lib_pack.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 7762fe12 2018-11-05 stsp const struct got_error *
106 7762fe12 2018-11-05 stsp got_object_qid_alloc_partial(struct got_object_qid **qid)
107 7762fe12 2018-11-05 stsp {
108 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
109 7762fe12 2018-11-05 stsp
110 7762fe12 2018-11-05 stsp *qid = malloc(sizeof(**qid));
111 7762fe12 2018-11-05 stsp if (*qid == NULL)
112 7762fe12 2018-11-05 stsp return got_error_from_errno();
113 7762fe12 2018-11-05 stsp
114 7762fe12 2018-11-05 stsp (*qid)->id = malloc(sizeof(*((*qid)->id)));
115 7762fe12 2018-11-05 stsp if ((*qid)->id == NULL) {
116 7762fe12 2018-11-05 stsp err = got_error_from_errno();
117 7762fe12 2018-11-05 stsp got_object_qid_free(*qid);
118 7762fe12 2018-11-05 stsp *qid = NULL;
119 7762fe12 2018-11-05 stsp }
120 7762fe12 2018-11-05 stsp return err;
121 7762fe12 2018-11-05 stsp }
122 7762fe12 2018-11-05 stsp
123 03fa71c8 2018-09-06 stsp void
124 03fa71c8 2018-09-06 stsp got_object_qid_free(struct got_object_qid *qid)
125 03fa71c8 2018-09-06 stsp {
126 03fa71c8 2018-09-06 stsp free(qid->id);
127 03fa71c8 2018-09-06 stsp free(qid);
128 876c234b 2018-09-10 stsp }
129 876c234b 2018-09-10 stsp
130 a440fac0 2018-09-06 stsp struct got_commit_object *
131 a440fac0 2018-09-06 stsp got_object_commit_alloc_partial(void)
132 a440fac0 2018-09-06 stsp {
133 a440fac0 2018-09-06 stsp struct got_commit_object *commit;
134 a440fac0 2018-09-06 stsp
135 a440fac0 2018-09-06 stsp commit = calloc(1, sizeof(*commit));
136 a440fac0 2018-09-06 stsp if (commit == NULL)
137 a440fac0 2018-09-06 stsp return NULL;
138 a440fac0 2018-09-06 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
139 a440fac0 2018-09-06 stsp if (commit->tree_id == NULL) {
140 a440fac0 2018-09-06 stsp free(commit);
141 a440fac0 2018-09-06 stsp return NULL;
142 a440fac0 2018-09-06 stsp }
143 a440fac0 2018-09-06 stsp
144 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&commit->parent_ids);
145 a440fac0 2018-09-06 stsp
146 a440fac0 2018-09-06 stsp return commit;
147 a440fac0 2018-09-06 stsp }
148 a440fac0 2018-09-06 stsp
149 05e1230b 2018-11-05 stsp struct got_mini_commit_object *
150 7762fe12 2018-11-05 stsp got_object_mini_commit_alloc_partial(void)
151 7762fe12 2018-11-05 stsp {
152 05e1230b 2018-11-05 stsp struct got_mini_commit_object *commit;
153 7762fe12 2018-11-05 stsp
154 7762fe12 2018-11-05 stsp commit = calloc(1, sizeof(*commit));
155 7762fe12 2018-11-05 stsp if (commit == NULL)
156 7762fe12 2018-11-05 stsp return NULL;
157 7762fe12 2018-11-05 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
158 7762fe12 2018-11-05 stsp if (commit->tree_id == NULL) {
159 7762fe12 2018-11-05 stsp free(commit);
160 7762fe12 2018-11-05 stsp return NULL;
161 7762fe12 2018-11-05 stsp }
162 7762fe12 2018-11-05 stsp
163 7762fe12 2018-11-05 stsp SIMPLEQ_INIT(&commit->parent_ids);
164 7762fe12 2018-11-05 stsp
165 7762fe12 2018-11-05 stsp return commit;
166 7762fe12 2018-11-05 stsp }
167 7762fe12 2018-11-05 stsp
168 a440fac0 2018-09-06 stsp const struct got_error *
169 a440fac0 2018-09-06 stsp got_object_commit_add_parent(struct got_commit_object *commit,
170 a440fac0 2018-09-06 stsp const char *id_str)
171 a440fac0 2018-09-06 stsp {
172 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
173 a440fac0 2018-09-06 stsp struct got_object_qid *qid;
174 a440fac0 2018-09-06 stsp
175 7762fe12 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
176 7762fe12 2018-11-05 stsp if (err)
177 a440fac0 2018-09-06 stsp return err;
178 7762fe12 2018-11-05 stsp
179 7762fe12 2018-11-05 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
180 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
181 7762fe12 2018-11-05 stsp free(qid->id);
182 7762fe12 2018-11-05 stsp free(qid);
183 7762fe12 2018-11-05 stsp return err;
184 a440fac0 2018-09-06 stsp }
185 a440fac0 2018-09-06 stsp
186 7762fe12 2018-11-05 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
187 7762fe12 2018-11-05 stsp commit->nparents++;
188 7762fe12 2018-11-05 stsp
189 7762fe12 2018-11-05 stsp return NULL;
190 7762fe12 2018-11-05 stsp }
191 7762fe12 2018-11-05 stsp
192 7762fe12 2018-11-05 stsp const struct got_error *
193 05e1230b 2018-11-05 stsp got_object_mini_commit_add_parent(struct got_mini_commit_object *commit,
194 7762fe12 2018-11-05 stsp const char *id_str)
195 7762fe12 2018-11-05 stsp {
196 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
197 7762fe12 2018-11-05 stsp struct got_object_qid *qid;
198 7762fe12 2018-11-05 stsp
199 7762fe12 2018-11-05 stsp err = got_object_qid_alloc_partial(&qid);
200 7762fe12 2018-11-05 stsp if (err)
201 7762fe12 2018-11-05 stsp return err;
202 7762fe12 2018-11-05 stsp
203 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
204 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
205 a440fac0 2018-09-06 stsp free(qid->id);
206 a440fac0 2018-09-06 stsp free(qid);
207 a440fac0 2018-09-06 stsp return err;
208 a440fac0 2018-09-06 stsp }
209 a440fac0 2018-09-06 stsp
210 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
211 a440fac0 2018-09-06 stsp commit->nparents++;
212 a440fac0 2018-09-06 stsp
213 a440fac0 2018-09-06 stsp return NULL;
214 a440fac0 2018-09-06 stsp }
215 a440fac0 2018-09-06 stsp
216 a440fac0 2018-09-06 stsp static const struct got_error *
217 a440fac0 2018-09-06 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
218 a440fac0 2018-09-06 stsp {
219 a440fac0 2018-09-06 stsp int sign = 1;
220 a440fac0 2018-09-06 stsp const char *p = tzstr;
221 a440fac0 2018-09-06 stsp time_t h, m;
222 a440fac0 2018-09-06 stsp
223 a440fac0 2018-09-06 stsp *gmtoff = 0;
224 a440fac0 2018-09-06 stsp
225 a440fac0 2018-09-06 stsp if (*p == '-')
226 a440fac0 2018-09-06 stsp sign = -1;
227 a440fac0 2018-09-06 stsp else if (*p != '+')
228 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
229 a440fac0 2018-09-06 stsp p++;
230 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
231 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
232 a440fac0 2018-09-06 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
233 a440fac0 2018-09-06 stsp
234 a440fac0 2018-09-06 stsp p += 2;
235 a440fac0 2018-09-06 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
236 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
237 a440fac0 2018-09-06 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
238 a440fac0 2018-09-06 stsp
239 a440fac0 2018-09-06 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
240 a440fac0 2018-09-06 stsp return NULL;
241 a440fac0 2018-09-06 stsp }
242 a440fac0 2018-09-06 stsp
243 a440fac0 2018-09-06 stsp static const struct got_error *
244 a440fac0 2018-09-06 stsp parse_commit_time(struct tm *tm, char *committer)
245 a440fac0 2018-09-06 stsp {
246 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
247 a440fac0 2018-09-06 stsp const char *errstr;
248 a440fac0 2018-09-06 stsp char *space, *tzstr;
249 a440fac0 2018-09-06 stsp time_t gmtoff;
250 a440fac0 2018-09-06 stsp time_t time;
251 a440fac0 2018-09-06 stsp
252 a440fac0 2018-09-06 stsp /* Parse and strip off trailing timezone indicator string. */
253 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
254 a440fac0 2018-09-06 stsp if (space == NULL)
255 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
256 a440fac0 2018-09-06 stsp tzstr = strdup(space + 1);
257 a440fac0 2018-09-06 stsp if (tzstr == NULL)
258 a440fac0 2018-09-06 stsp return got_error_from_errno();
259 a440fac0 2018-09-06 stsp err = parse_gmtoff(&gmtoff, tzstr);
260 a440fac0 2018-09-06 stsp free(tzstr);
261 a440fac0 2018-09-06 stsp if (err)
262 a440fac0 2018-09-06 stsp return err;
263 a440fac0 2018-09-06 stsp *space = '\0';
264 a440fac0 2018-09-06 stsp
265 a440fac0 2018-09-06 stsp /* Timestamp is separated from committer name + email by space. */
266 a440fac0 2018-09-06 stsp space = strrchr(committer, ' ');
267 a440fac0 2018-09-06 stsp if (space == NULL)
268 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
269 a440fac0 2018-09-06 stsp
270 a440fac0 2018-09-06 stsp /* Timestamp parsed here is expressed in comitter's local time. */
271 a440fac0 2018-09-06 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
272 a440fac0 2018-09-06 stsp if (errstr)
273 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
274 a440fac0 2018-09-06 stsp
275 a440fac0 2018-09-06 stsp /* Express the time stamp in UTC. */
276 a440fac0 2018-09-06 stsp memset(tm, 0, sizeof(*tm));
277 a440fac0 2018-09-06 stsp time -= gmtoff;
278 a440fac0 2018-09-06 stsp if (localtime_r(&time, tm) == NULL)
279 a440fac0 2018-09-06 stsp return got_error_from_errno();
280 a440fac0 2018-09-06 stsp tm->tm_gmtoff = gmtoff;
281 a440fac0 2018-09-06 stsp
282 a440fac0 2018-09-06 stsp /* Strip off parsed time information, leaving just author and email. */
283 a440fac0 2018-09-06 stsp *space = '\0';
284 a440fac0 2018-09-06 stsp
285 a440fac0 2018-09-06 stsp return NULL;
286 a440fac0 2018-09-06 stsp }
287 a440fac0 2018-09-06 stsp
288 03fa71c8 2018-09-06 stsp void
289 03fa71c8 2018-09-06 stsp got_object_commit_close(struct got_commit_object *commit)
290 03fa71c8 2018-09-06 stsp {
291 03fa71c8 2018-09-06 stsp struct got_object_qid *qid;
292 03fa71c8 2018-09-06 stsp
293 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0) {
294 03fa71c8 2018-09-06 stsp commit->refcnt--;
295 03fa71c8 2018-09-06 stsp if (commit->refcnt > 0)
296 03fa71c8 2018-09-06 stsp return;
297 03fa71c8 2018-09-06 stsp }
298 03fa71c8 2018-09-06 stsp
299 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
300 03fa71c8 2018-09-06 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
301 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
302 03fa71c8 2018-09-06 stsp got_object_qid_free(qid);
303 03fa71c8 2018-09-06 stsp }
304 03fa71c8 2018-09-06 stsp
305 03fa71c8 2018-09-06 stsp free(commit->tree_id);
306 03fa71c8 2018-09-06 stsp free(commit->author);
307 03fa71c8 2018-09-06 stsp free(commit->committer);
308 03fa71c8 2018-09-06 stsp free(commit->logmsg);
309 03fa71c8 2018-09-06 stsp free(commit);
310 03fa71c8 2018-09-06 stsp }
311 03fa71c8 2018-09-06 stsp
312 7762fe12 2018-11-05 stsp void
313 05e1230b 2018-11-05 stsp got_object_mini_commit_close(struct got_mini_commit_object *commit)
314 7762fe12 2018-11-05 stsp {
315 7762fe12 2018-11-05 stsp struct got_object_qid *qid;
316 e32baab7 2018-11-05 stsp
317 e32baab7 2018-11-05 stsp if (commit->refcnt > 0) {
318 e32baab7 2018-11-05 stsp commit->refcnt--;
319 e32baab7 2018-11-05 stsp if (commit->refcnt > 0)
320 e32baab7 2018-11-05 stsp return;
321 e32baab7 2018-11-05 stsp }
322 7762fe12 2018-11-05 stsp
323 7762fe12 2018-11-05 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
324 7762fe12 2018-11-05 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
325 7762fe12 2018-11-05 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
326 7762fe12 2018-11-05 stsp got_object_qid_free(qid);
327 7762fe12 2018-11-05 stsp }
328 7762fe12 2018-11-05 stsp
329 7762fe12 2018-11-05 stsp free(commit->tree_id);
330 7762fe12 2018-11-05 stsp free(commit);
331 7762fe12 2018-11-05 stsp }
332 7762fe12 2018-11-05 stsp
333 a440fac0 2018-09-06 stsp const struct got_error *
334 7762fe12 2018-11-05 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
335 7762fe12 2018-11-05 stsp size_t len)
336 a440fac0 2018-09-06 stsp {
337 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
338 a440fac0 2018-09-06 stsp char *s = buf;
339 a440fac0 2018-09-06 stsp size_t tlen;
340 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
341 7762fe12 2018-11-05 stsp
342 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
343 a440fac0 2018-09-06 stsp if (*commit == NULL)
344 a440fac0 2018-09-06 stsp return got_error_from_errno();
345 a440fac0 2018-09-06 stsp
346 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
347 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
348 a440fac0 2018-09-06 stsp remain -= tlen;
349 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
350 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
351 a440fac0 2018-09-06 stsp goto done;
352 a440fac0 2018-09-06 stsp }
353 a440fac0 2018-09-06 stsp s += tlen;
354 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
355 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
356 a440fac0 2018-09-06 stsp goto done;
357 a440fac0 2018-09-06 stsp }
358 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
359 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
360 a440fac0 2018-09-06 stsp } else {
361 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
362 a440fac0 2018-09-06 stsp goto done;
363 a440fac0 2018-09-06 stsp }
364 a440fac0 2018-09-06 stsp
365 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
366 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
367 a440fac0 2018-09-06 stsp remain -= tlen;
368 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
369 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
370 a440fac0 2018-09-06 stsp goto done;
371 a440fac0 2018-09-06 stsp }
372 a440fac0 2018-09-06 stsp s += tlen;
373 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
374 a440fac0 2018-09-06 stsp if (err)
375 a440fac0 2018-09-06 stsp goto done;
376 a440fac0 2018-09-06 stsp
377 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
378 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
379 a440fac0 2018-09-06 stsp }
380 a440fac0 2018-09-06 stsp
381 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
382 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
383 a440fac0 2018-09-06 stsp char *p;
384 a440fac0 2018-09-06 stsp size_t slen;
385 a440fac0 2018-09-06 stsp
386 a440fac0 2018-09-06 stsp remain -= tlen;
387 a440fac0 2018-09-06 stsp if (remain <= 0) {
388 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
389 a440fac0 2018-09-06 stsp goto done;
390 a440fac0 2018-09-06 stsp }
391 a440fac0 2018-09-06 stsp s += tlen;
392 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
393 a440fac0 2018-09-06 stsp if (p == NULL) {
394 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
395 a440fac0 2018-09-06 stsp goto done;
396 a440fac0 2018-09-06 stsp }
397 a440fac0 2018-09-06 stsp *p = '\0';
398 a440fac0 2018-09-06 stsp slen = strlen(s);
399 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
400 a440fac0 2018-09-06 stsp if (err)
401 a440fac0 2018-09-06 stsp goto done;
402 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
403 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
404 a440fac0 2018-09-06 stsp err = got_error_from_errno();
405 a440fac0 2018-09-06 stsp goto done;
406 a440fac0 2018-09-06 stsp }
407 a440fac0 2018-09-06 stsp s += slen + 1;
408 a440fac0 2018-09-06 stsp remain -= slen + 1;
409 a440fac0 2018-09-06 stsp }
410 a440fac0 2018-09-06 stsp
411 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
412 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
413 a440fac0 2018-09-06 stsp char *p;
414 a440fac0 2018-09-06 stsp size_t slen;
415 a440fac0 2018-09-06 stsp
416 a440fac0 2018-09-06 stsp remain -= tlen;
417 a440fac0 2018-09-06 stsp if (remain <= 0) {
418 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
419 a440fac0 2018-09-06 stsp goto done;
420 a440fac0 2018-09-06 stsp }
421 a440fac0 2018-09-06 stsp s += tlen;
422 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
423 a440fac0 2018-09-06 stsp if (p == NULL) {
424 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
425 a440fac0 2018-09-06 stsp goto done;
426 a440fac0 2018-09-06 stsp }
427 a440fac0 2018-09-06 stsp *p = '\0';
428 a440fac0 2018-09-06 stsp slen = strlen(s);
429 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
430 a440fac0 2018-09-06 stsp if (err)
431 a440fac0 2018-09-06 stsp goto done;
432 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
433 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
434 a440fac0 2018-09-06 stsp err = got_error_from_errno();
435 a440fac0 2018-09-06 stsp goto done;
436 a440fac0 2018-09-06 stsp }
437 a440fac0 2018-09-06 stsp s += slen + 1;
438 a440fac0 2018-09-06 stsp remain -= slen + 1;
439 a440fac0 2018-09-06 stsp }
440 a440fac0 2018-09-06 stsp
441 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
442 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
443 a440fac0 2018-09-06 stsp err = got_error_from_errno();
444 a440fac0 2018-09-06 stsp goto done;
445 a440fac0 2018-09-06 stsp }
446 a440fac0 2018-09-06 stsp done:
447 a440fac0 2018-09-06 stsp if (err) {
448 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
449 a440fac0 2018-09-06 stsp *commit = NULL;
450 a440fac0 2018-09-06 stsp }
451 a440fac0 2018-09-06 stsp return err;
452 a440fac0 2018-09-06 stsp }
453 a440fac0 2018-09-06 stsp
454 7762fe12 2018-11-05 stsp const struct got_error *
455 05e1230b 2018-11-05 stsp got_object_parse_mini_commit(struct got_mini_commit_object **commit, char *buf,
456 7762fe12 2018-11-05 stsp size_t len)
457 7762fe12 2018-11-05 stsp {
458 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
459 7762fe12 2018-11-05 stsp char *s = buf;
460 7762fe12 2018-11-05 stsp size_t tlen;
461 7762fe12 2018-11-05 stsp ssize_t remain = (ssize_t)len;
462 7762fe12 2018-11-05 stsp
463 7762fe12 2018-11-05 stsp *commit = got_object_mini_commit_alloc_partial();
464 7762fe12 2018-11-05 stsp if (*commit == NULL)
465 7762fe12 2018-11-05 stsp return got_error_from_errno();
466 7762fe12 2018-11-05 stsp
467 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
468 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
469 7762fe12 2018-11-05 stsp remain -= tlen;
470 7762fe12 2018-11-05 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
471 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 7762fe12 2018-11-05 stsp goto done;
473 7762fe12 2018-11-05 stsp }
474 7762fe12 2018-11-05 stsp s += tlen;
475 7762fe12 2018-11-05 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
476 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
477 7762fe12 2018-11-05 stsp goto done;
478 7762fe12 2018-11-05 stsp }
479 7762fe12 2018-11-05 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
480 7762fe12 2018-11-05 stsp s += SHA1_DIGEST_STRING_LENGTH;
481 7762fe12 2018-11-05 stsp } else {
482 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
483 7762fe12 2018-11-05 stsp goto done;
484 7762fe12 2018-11-05 stsp }
485 7762fe12 2018-11-05 stsp
486 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
487 7762fe12 2018-11-05 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
488 7762fe12 2018-11-05 stsp remain -= tlen;
489 7762fe12 2018-11-05 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
490 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 7762fe12 2018-11-05 stsp goto done;
492 7762fe12 2018-11-05 stsp }
493 7762fe12 2018-11-05 stsp s += tlen;
494 7762fe12 2018-11-05 stsp err = got_object_mini_commit_add_parent(*commit, s);
495 7762fe12 2018-11-05 stsp if (err)
496 7762fe12 2018-11-05 stsp goto done;
497 7762fe12 2018-11-05 stsp
498 7762fe12 2018-11-05 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
499 7762fe12 2018-11-05 stsp s += SHA1_DIGEST_STRING_LENGTH;
500 7762fe12 2018-11-05 stsp }
501 7762fe12 2018-11-05 stsp
502 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
503 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
504 7762fe12 2018-11-05 stsp char *p;
505 7762fe12 2018-11-05 stsp size_t slen;
506 7762fe12 2018-11-05 stsp
507 7762fe12 2018-11-05 stsp remain -= tlen;
508 7762fe12 2018-11-05 stsp if (remain <= 0) {
509 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
510 7762fe12 2018-11-05 stsp goto done;
511 7762fe12 2018-11-05 stsp }
512 7762fe12 2018-11-05 stsp s += tlen;
513 7762fe12 2018-11-05 stsp p = strchr(s, '\n');
514 7762fe12 2018-11-05 stsp if (p == NULL) {
515 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
516 7762fe12 2018-11-05 stsp goto done;
517 7762fe12 2018-11-05 stsp }
518 7762fe12 2018-11-05 stsp *p = '\0';
519 7762fe12 2018-11-05 stsp slen = strlen(s);
520 7762fe12 2018-11-05 stsp s += slen + 1;
521 7762fe12 2018-11-05 stsp remain -= slen + 1;
522 7762fe12 2018-11-05 stsp }
523 7762fe12 2018-11-05 stsp
524 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
526 7762fe12 2018-11-05 stsp char *p;
527 7762fe12 2018-11-05 stsp size_t slen;
528 7762fe12 2018-11-05 stsp
529 7762fe12 2018-11-05 stsp remain -= tlen;
530 7762fe12 2018-11-05 stsp if (remain <= 0) {
531 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
532 7762fe12 2018-11-05 stsp goto done;
533 7762fe12 2018-11-05 stsp }
534 7762fe12 2018-11-05 stsp s += tlen;
535 7762fe12 2018-11-05 stsp p = strchr(s, '\n');
536 7762fe12 2018-11-05 stsp if (p == NULL) {
537 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
538 7762fe12 2018-11-05 stsp goto done;
539 7762fe12 2018-11-05 stsp }
540 7762fe12 2018-11-05 stsp *p = '\0';
541 7762fe12 2018-11-05 stsp slen = strlen(s);
542 7762fe12 2018-11-05 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
543 7762fe12 2018-11-05 stsp if (err)
544 7762fe12 2018-11-05 stsp goto done;
545 7762fe12 2018-11-05 stsp s += slen + 1;
546 7762fe12 2018-11-05 stsp remain -= slen + 1;
547 7762fe12 2018-11-05 stsp }
548 7762fe12 2018-11-05 stsp
549 7762fe12 2018-11-05 stsp done:
550 7762fe12 2018-11-05 stsp if (err) {
551 7762fe12 2018-11-05 stsp got_object_mini_commit_close(*commit);
552 7762fe12 2018-11-05 stsp *commit = NULL;
553 7762fe12 2018-11-05 stsp }
554 7762fe12 2018-11-05 stsp return err;
555 7762fe12 2018-11-05 stsp }
556 7762fe12 2018-11-05 stsp
557 ad242220 2018-09-08 stsp void
558 ad242220 2018-09-08 stsp got_object_tree_entry_close(struct got_tree_entry *te)
559 a440fac0 2018-09-06 stsp {
560 a440fac0 2018-09-06 stsp free(te->id);
561 a440fac0 2018-09-06 stsp free(te->name);
562 a440fac0 2018-09-06 stsp free(te);
563 a440fac0 2018-09-06 stsp }
564 a440fac0 2018-09-06 stsp
565 03fa71c8 2018-09-06 stsp void
566 03fa71c8 2018-09-06 stsp got_object_tree_close(struct got_tree_object *tree)
567 03fa71c8 2018-09-06 stsp {
568 03fa71c8 2018-09-06 stsp struct got_tree_entry *te;
569 03fa71c8 2018-09-06 stsp
570 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0) {
571 03fa71c8 2018-09-06 stsp tree->refcnt--;
572 03fa71c8 2018-09-06 stsp if (tree->refcnt > 0)
573 03fa71c8 2018-09-06 stsp return;
574 03fa71c8 2018-09-06 stsp }
575 03fa71c8 2018-09-06 stsp
576 03fa71c8 2018-09-06 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
577 03fa71c8 2018-09-06 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
578 03fa71c8 2018-09-06 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
579 ad242220 2018-09-08 stsp got_object_tree_entry_close(te);
580 03fa71c8 2018-09-06 stsp }
581 03fa71c8 2018-09-06 stsp
582 03fa71c8 2018-09-06 stsp free(tree);
583 03fa71c8 2018-09-06 stsp }
584 03fa71c8 2018-09-06 stsp
585 a440fac0 2018-09-06 stsp struct got_tree_entry *
586 a440fac0 2018-09-06 stsp got_alloc_tree_entry_partial(void)
587 a440fac0 2018-09-06 stsp {
588 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
589 a440fac0 2018-09-06 stsp
590 a440fac0 2018-09-06 stsp te = calloc(1, sizeof(*te));
591 a440fac0 2018-09-06 stsp if (te == NULL)
592 a440fac0 2018-09-06 stsp return NULL;
593 a440fac0 2018-09-06 stsp
594 a440fac0 2018-09-06 stsp te->id = calloc(1, sizeof(*te->id));
595 a440fac0 2018-09-06 stsp if (te->id == NULL) {
596 a440fac0 2018-09-06 stsp free(te);
597 a440fac0 2018-09-06 stsp te = NULL;
598 a440fac0 2018-09-06 stsp }
599 a440fac0 2018-09-06 stsp return te;
600 a440fac0 2018-09-06 stsp }
601 a440fac0 2018-09-06 stsp
602 a440fac0 2018-09-06 stsp static const struct got_error *
603 a440fac0 2018-09-06 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
604 a440fac0 2018-09-06 stsp size_t maxlen)
605 a440fac0 2018-09-06 stsp {
606 a440fac0 2018-09-06 stsp char *p = buf, *space;
607 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
608 a440fac0 2018-09-06 stsp
609 a440fac0 2018-09-06 stsp *te = got_alloc_tree_entry_partial();
610 a440fac0 2018-09-06 stsp if (*te == NULL)
611 a440fac0 2018-09-06 stsp return got_error_from_errno();
612 a440fac0 2018-09-06 stsp
613 a440fac0 2018-09-06 stsp *elen = strlen(buf) + 1;
614 a440fac0 2018-09-06 stsp if (*elen > maxlen) {
615 a440fac0 2018-09-06 stsp free(*te);
616 a440fac0 2018-09-06 stsp *te = NULL;
617 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
618 a440fac0 2018-09-06 stsp }
619 a440fac0 2018-09-06 stsp
620 a440fac0 2018-09-06 stsp space = strchr(buf, ' ');
621 a440fac0 2018-09-06 stsp if (space == NULL) {
622 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
623 a440fac0 2018-09-06 stsp free(*te);
624 a440fac0 2018-09-06 stsp *te = NULL;
625 a440fac0 2018-09-06 stsp return err;
626 a440fac0 2018-09-06 stsp }
627 a440fac0 2018-09-06 stsp while (*p != ' ') {
628 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
629 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
630 a440fac0 2018-09-06 stsp goto done;
631 a440fac0 2018-09-06 stsp }
632 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
633 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
634 a440fac0 2018-09-06 stsp p++;
635 a440fac0 2018-09-06 stsp }
636 a440fac0 2018-09-06 stsp
637 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
638 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
639 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
640 a440fac0 2018-09-06 stsp goto done;
641 a440fac0 2018-09-06 stsp }
642 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
643 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
644 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
645 a440fac0 2018-09-06 stsp done:
646 a440fac0 2018-09-06 stsp if (err) {
647 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
648 a440fac0 2018-09-06 stsp *te = NULL;
649 a440fac0 2018-09-06 stsp }
650 a440fac0 2018-09-06 stsp return err;
651 a440fac0 2018-09-06 stsp }
652 a440fac0 2018-09-06 stsp
653 a440fac0 2018-09-06 stsp const struct got_error *
654 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
655 a440fac0 2018-09-06 stsp {
656 a440fac0 2018-09-06 stsp const struct got_error *err;
657 a440fac0 2018-09-06 stsp size_t remain = len;
658 a440fac0 2018-09-06 stsp
659 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
660 a440fac0 2018-09-06 stsp if (*tree == NULL)
661 a440fac0 2018-09-06 stsp return got_error_from_errno();
662 a440fac0 2018-09-06 stsp
663 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
664 a440fac0 2018-09-06 stsp
665 a440fac0 2018-09-06 stsp while (remain > 0) {
666 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
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 a440fac0 2018-09-06 stsp return err;
672 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
673 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
674 a440fac0 2018-09-06 stsp buf += elen;
675 a440fac0 2018-09-06 stsp remain -= elen;
676 a440fac0 2018-09-06 stsp }
677 a440fac0 2018-09-06 stsp
678 a440fac0 2018-09-06 stsp if (remain != 0) {
679 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
680 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
681 a440fac0 2018-09-06 stsp }
682 a440fac0 2018-09-06 stsp
683 a440fac0 2018-09-06 stsp return NULL;
684 a440fac0 2018-09-06 stsp }
685 a440fac0 2018-09-06 stsp
686 ad242220 2018-09-08 stsp const struct got_error *
687 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
688 a440fac0 2018-09-06 stsp {
689 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
690 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
691 a440fac0 2018-09-06 stsp size_t n, total, remain;
692 a440fac0 2018-09-06 stsp uint8_t *buf;
693 a440fac0 2018-09-06 stsp
694 a440fac0 2018-09-06 stsp *outbuf = NULL;
695 a440fac0 2018-09-06 stsp *outlen = 0;
696 a440fac0 2018-09-06 stsp
697 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
698 a440fac0 2018-09-06 stsp if (buf == NULL)
699 a440fac0 2018-09-06 stsp return got_error_from_errno();
700 a440fac0 2018-09-06 stsp
701 a440fac0 2018-09-06 stsp remain = blocksize;
702 a440fac0 2018-09-06 stsp total = 0;
703 a440fac0 2018-09-06 stsp while (1) {
704 a440fac0 2018-09-06 stsp if (remain == 0) {
705 a440fac0 2018-09-06 stsp uint8_t *newbuf;
706 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
707 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
708 a440fac0 2018-09-06 stsp err = got_error_from_errno();
709 a440fac0 2018-09-06 stsp goto done;
710 a440fac0 2018-09-06 stsp }
711 a440fac0 2018-09-06 stsp buf = newbuf;
712 a440fac0 2018-09-06 stsp remain += blocksize;
713 a440fac0 2018-09-06 stsp }
714 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
715 a440fac0 2018-09-06 stsp if (n == 0) {
716 a440fac0 2018-09-06 stsp if (ferror(f)) {
717 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
718 a440fac0 2018-09-06 stsp goto done;
719 a440fac0 2018-09-06 stsp }
720 a440fac0 2018-09-06 stsp break; /* EOF */
721 a440fac0 2018-09-06 stsp }
722 a440fac0 2018-09-06 stsp remain -= n;
723 a440fac0 2018-09-06 stsp total += n;
724 a440fac0 2018-09-06 stsp };
725 a440fac0 2018-09-06 stsp
726 a440fac0 2018-09-06 stsp done:
727 a440fac0 2018-09-06 stsp if (err == NULL) {
728 a440fac0 2018-09-06 stsp *outbuf = buf;
729 a440fac0 2018-09-06 stsp *outlen = total;
730 a440fac0 2018-09-06 stsp } else
731 a440fac0 2018-09-06 stsp free(buf);
732 ad242220 2018-09-08 stsp return err;
733 a440fac0 2018-09-06 stsp }