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 7762fe12 2018-11-05 stsp struct got_commit_object_mini *
150 7762fe12 2018-11-05 stsp got_object_mini_commit_alloc_partial(void)
151 7762fe12 2018-11-05 stsp {
152 7762fe12 2018-11-05 stsp struct got_commit_object_mini *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 7762fe12 2018-11-05 stsp got_object_mini_commit_add_parent(struct got_commit_object_mini *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 7762fe12 2018-11-05 stsp got_object_mini_commit_close(struct got_commit_object_mini *commit)
314 7762fe12 2018-11-05 stsp {
315 7762fe12 2018-11-05 stsp struct got_object_qid *qid;
316 7762fe12 2018-11-05 stsp
317 7762fe12 2018-11-05 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
318 7762fe12 2018-11-05 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
319 7762fe12 2018-11-05 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
320 7762fe12 2018-11-05 stsp got_object_qid_free(qid);
321 7762fe12 2018-11-05 stsp }
322 7762fe12 2018-11-05 stsp
323 7762fe12 2018-11-05 stsp free(commit->tree_id);
324 7762fe12 2018-11-05 stsp free(commit);
325 7762fe12 2018-11-05 stsp }
326 7762fe12 2018-11-05 stsp
327 a440fac0 2018-09-06 stsp const struct got_error *
328 7762fe12 2018-11-05 stsp got_object_parse_commit(struct got_commit_object **commit, char *buf,
329 7762fe12 2018-11-05 stsp size_t len)
330 a440fac0 2018-09-06 stsp {
331 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
332 a440fac0 2018-09-06 stsp char *s = buf;
333 a440fac0 2018-09-06 stsp size_t tlen;
334 a440fac0 2018-09-06 stsp ssize_t remain = (ssize_t)len;
335 7762fe12 2018-11-05 stsp
336 a440fac0 2018-09-06 stsp *commit = got_object_commit_alloc_partial();
337 a440fac0 2018-09-06 stsp if (*commit == NULL)
338 a440fac0 2018-09-06 stsp return got_error_from_errno();
339 a440fac0 2018-09-06 stsp
340 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
341 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
342 a440fac0 2018-09-06 stsp remain -= tlen;
343 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
344 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
345 a440fac0 2018-09-06 stsp goto done;
346 a440fac0 2018-09-06 stsp }
347 a440fac0 2018-09-06 stsp s += tlen;
348 a440fac0 2018-09-06 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
349 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
350 a440fac0 2018-09-06 stsp goto done;
351 a440fac0 2018-09-06 stsp }
352 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
353 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
354 a440fac0 2018-09-06 stsp } else {
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
359 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
360 a440fac0 2018-09-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
361 a440fac0 2018-09-06 stsp remain -= tlen;
362 a440fac0 2018-09-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
363 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
364 a440fac0 2018-09-06 stsp goto done;
365 a440fac0 2018-09-06 stsp }
366 a440fac0 2018-09-06 stsp s += tlen;
367 a440fac0 2018-09-06 stsp err = got_object_commit_add_parent(*commit, s);
368 a440fac0 2018-09-06 stsp if (err)
369 a440fac0 2018-09-06 stsp goto done;
370 a440fac0 2018-09-06 stsp
371 a440fac0 2018-09-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
372 a440fac0 2018-09-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
373 a440fac0 2018-09-06 stsp }
374 a440fac0 2018-09-06 stsp
375 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
376 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
377 a440fac0 2018-09-06 stsp char *p;
378 a440fac0 2018-09-06 stsp size_t slen;
379 a440fac0 2018-09-06 stsp
380 a440fac0 2018-09-06 stsp remain -= tlen;
381 a440fac0 2018-09-06 stsp if (remain <= 0) {
382 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
383 a440fac0 2018-09-06 stsp goto done;
384 a440fac0 2018-09-06 stsp }
385 a440fac0 2018-09-06 stsp s += tlen;
386 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
387 a440fac0 2018-09-06 stsp if (p == NULL) {
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 *p = '\0';
392 a440fac0 2018-09-06 stsp slen = strlen(s);
393 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_author, s);
394 a440fac0 2018-09-06 stsp if (err)
395 a440fac0 2018-09-06 stsp goto done;
396 a440fac0 2018-09-06 stsp (*commit)->author = strdup(s);
397 a440fac0 2018-09-06 stsp if ((*commit)->author == NULL) {
398 a440fac0 2018-09-06 stsp err = got_error_from_errno();
399 a440fac0 2018-09-06 stsp goto done;
400 a440fac0 2018-09-06 stsp }
401 a440fac0 2018-09-06 stsp s += slen + 1;
402 a440fac0 2018-09-06 stsp remain -= slen + 1;
403 a440fac0 2018-09-06 stsp }
404 a440fac0 2018-09-06 stsp
405 a440fac0 2018-09-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
406 a440fac0 2018-09-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
407 a440fac0 2018-09-06 stsp char *p;
408 a440fac0 2018-09-06 stsp size_t slen;
409 a440fac0 2018-09-06 stsp
410 a440fac0 2018-09-06 stsp remain -= tlen;
411 a440fac0 2018-09-06 stsp if (remain <= 0) {
412 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
413 a440fac0 2018-09-06 stsp goto done;
414 a440fac0 2018-09-06 stsp }
415 a440fac0 2018-09-06 stsp s += tlen;
416 a440fac0 2018-09-06 stsp p = strchr(s, '\n');
417 a440fac0 2018-09-06 stsp if (p == NULL) {
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 *p = '\0';
422 a440fac0 2018-09-06 stsp slen = strlen(s);
423 a440fac0 2018-09-06 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
424 a440fac0 2018-09-06 stsp if (err)
425 a440fac0 2018-09-06 stsp goto done;
426 a440fac0 2018-09-06 stsp (*commit)->committer = strdup(s);
427 a440fac0 2018-09-06 stsp if ((*commit)->committer == NULL) {
428 a440fac0 2018-09-06 stsp err = got_error_from_errno();
429 a440fac0 2018-09-06 stsp goto done;
430 a440fac0 2018-09-06 stsp }
431 a440fac0 2018-09-06 stsp s += slen + 1;
432 a440fac0 2018-09-06 stsp remain -= slen + 1;
433 a440fac0 2018-09-06 stsp }
434 a440fac0 2018-09-06 stsp
435 a440fac0 2018-09-06 stsp (*commit)->logmsg = strndup(s, remain);
436 a440fac0 2018-09-06 stsp if ((*commit)->logmsg == NULL) {
437 a440fac0 2018-09-06 stsp err = got_error_from_errno();
438 a440fac0 2018-09-06 stsp goto done;
439 a440fac0 2018-09-06 stsp }
440 a440fac0 2018-09-06 stsp done:
441 a440fac0 2018-09-06 stsp if (err) {
442 a440fac0 2018-09-06 stsp got_object_commit_close(*commit);
443 a440fac0 2018-09-06 stsp *commit = NULL;
444 a440fac0 2018-09-06 stsp }
445 a440fac0 2018-09-06 stsp return err;
446 a440fac0 2018-09-06 stsp }
447 a440fac0 2018-09-06 stsp
448 7762fe12 2018-11-05 stsp const struct got_error *
449 7762fe12 2018-11-05 stsp got_object_parse_mini_commit(struct got_commit_object_mini **commit, char *buf,
450 7762fe12 2018-11-05 stsp size_t len)
451 7762fe12 2018-11-05 stsp {
452 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
453 7762fe12 2018-11-05 stsp char *s = buf;
454 7762fe12 2018-11-05 stsp size_t tlen;
455 7762fe12 2018-11-05 stsp ssize_t remain = (ssize_t)len;
456 7762fe12 2018-11-05 stsp
457 7762fe12 2018-11-05 stsp *commit = got_object_mini_commit_alloc_partial();
458 7762fe12 2018-11-05 stsp if (*commit == NULL)
459 7762fe12 2018-11-05 stsp return got_error_from_errno();
460 7762fe12 2018-11-05 stsp
461 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
462 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
463 7762fe12 2018-11-05 stsp remain -= tlen;
464 7762fe12 2018-11-05 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
465 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
466 7762fe12 2018-11-05 stsp goto done;
467 7762fe12 2018-11-05 stsp }
468 7762fe12 2018-11-05 stsp s += tlen;
469 7762fe12 2018-11-05 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
470 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
471 7762fe12 2018-11-05 stsp goto done;
472 7762fe12 2018-11-05 stsp }
473 7762fe12 2018-11-05 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
474 7762fe12 2018-11-05 stsp s += SHA1_DIGEST_STRING_LENGTH;
475 7762fe12 2018-11-05 stsp } else {
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
480 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
481 7762fe12 2018-11-05 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
482 7762fe12 2018-11-05 stsp remain -= tlen;
483 7762fe12 2018-11-05 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
484 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 7762fe12 2018-11-05 stsp goto done;
486 7762fe12 2018-11-05 stsp }
487 7762fe12 2018-11-05 stsp s += tlen;
488 7762fe12 2018-11-05 stsp err = got_object_mini_commit_add_parent(*commit, s);
489 7762fe12 2018-11-05 stsp if (err)
490 7762fe12 2018-11-05 stsp goto done;
491 7762fe12 2018-11-05 stsp
492 7762fe12 2018-11-05 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
493 7762fe12 2018-11-05 stsp s += SHA1_DIGEST_STRING_LENGTH;
494 7762fe12 2018-11-05 stsp }
495 7762fe12 2018-11-05 stsp
496 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
497 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
498 7762fe12 2018-11-05 stsp char *p;
499 7762fe12 2018-11-05 stsp size_t slen;
500 7762fe12 2018-11-05 stsp
501 7762fe12 2018-11-05 stsp remain -= tlen;
502 7762fe12 2018-11-05 stsp if (remain <= 0) {
503 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 7762fe12 2018-11-05 stsp goto done;
505 7762fe12 2018-11-05 stsp }
506 7762fe12 2018-11-05 stsp s += tlen;
507 7762fe12 2018-11-05 stsp p = strchr(s, '\n');
508 7762fe12 2018-11-05 stsp if (p == NULL) {
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 *p = '\0';
513 7762fe12 2018-11-05 stsp slen = strlen(s);
514 7762fe12 2018-11-05 stsp s += slen + 1;
515 7762fe12 2018-11-05 stsp remain -= slen + 1;
516 7762fe12 2018-11-05 stsp }
517 7762fe12 2018-11-05 stsp
518 7762fe12 2018-11-05 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
519 7762fe12 2018-11-05 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
520 7762fe12 2018-11-05 stsp char *p;
521 7762fe12 2018-11-05 stsp size_t slen;
522 7762fe12 2018-11-05 stsp
523 7762fe12 2018-11-05 stsp remain -= tlen;
524 7762fe12 2018-11-05 stsp if (remain <= 0) {
525 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
526 7762fe12 2018-11-05 stsp goto done;
527 7762fe12 2018-11-05 stsp }
528 7762fe12 2018-11-05 stsp s += tlen;
529 7762fe12 2018-11-05 stsp p = strchr(s, '\n');
530 7762fe12 2018-11-05 stsp if (p == NULL) {
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 *p = '\0';
535 7762fe12 2018-11-05 stsp slen = strlen(s);
536 7762fe12 2018-11-05 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
537 7762fe12 2018-11-05 stsp if (err)
538 7762fe12 2018-11-05 stsp goto done;
539 7762fe12 2018-11-05 stsp s += slen + 1;
540 7762fe12 2018-11-05 stsp remain -= slen + 1;
541 7762fe12 2018-11-05 stsp }
542 7762fe12 2018-11-05 stsp
543 7762fe12 2018-11-05 stsp done:
544 7762fe12 2018-11-05 stsp if (err) {
545 7762fe12 2018-11-05 stsp got_object_mini_commit_close(*commit);
546 7762fe12 2018-11-05 stsp *commit = NULL;
547 7762fe12 2018-11-05 stsp }
548 7762fe12 2018-11-05 stsp return err;
549 7762fe12 2018-11-05 stsp }
550 7762fe12 2018-11-05 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 a440fac0 2018-09-06 stsp te = calloc(1, 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 a440fac0 2018-09-06 stsp te->id = calloc(1, 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 a440fac0 2018-09-06 stsp while (*p != ' ') {
622 a440fac0 2018-09-06 stsp if (*p < '0' && *p > '7') {
623 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 a440fac0 2018-09-06 stsp goto done;
625 a440fac0 2018-09-06 stsp }
626 a440fac0 2018-09-06 stsp (*te)->mode <<= 3;
627 a440fac0 2018-09-06 stsp (*te)->mode |= *p - '0';
628 a440fac0 2018-09-06 stsp p++;
629 a440fac0 2018-09-06 stsp }
630 a440fac0 2018-09-06 stsp
631 a440fac0 2018-09-06 stsp (*te)->name = strdup(space + 1);
632 a440fac0 2018-09-06 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
633 a440fac0 2018-09-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
634 a440fac0 2018-09-06 stsp goto done;
635 a440fac0 2018-09-06 stsp }
636 a440fac0 2018-09-06 stsp buf += strlen(buf) + 1;
637 a440fac0 2018-09-06 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
638 a440fac0 2018-09-06 stsp *elen += SHA1_DIGEST_LENGTH;
639 a440fac0 2018-09-06 stsp done:
640 a440fac0 2018-09-06 stsp if (err) {
641 ad242220 2018-09-08 stsp got_object_tree_entry_close(*te);
642 a440fac0 2018-09-06 stsp *te = NULL;
643 a440fac0 2018-09-06 stsp }
644 a440fac0 2018-09-06 stsp return err;
645 a440fac0 2018-09-06 stsp }
646 a440fac0 2018-09-06 stsp
647 a440fac0 2018-09-06 stsp const struct got_error *
648 a440fac0 2018-09-06 stsp got_object_parse_tree(struct got_tree_object **tree, uint8_t *buf, size_t len)
649 a440fac0 2018-09-06 stsp {
650 a440fac0 2018-09-06 stsp const struct got_error *err;
651 a440fac0 2018-09-06 stsp size_t remain = len;
652 a440fac0 2018-09-06 stsp
653 a440fac0 2018-09-06 stsp *tree = calloc(1, sizeof(**tree));
654 a440fac0 2018-09-06 stsp if (*tree == NULL)
655 a440fac0 2018-09-06 stsp return got_error_from_errno();
656 a440fac0 2018-09-06 stsp
657 a440fac0 2018-09-06 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
658 a440fac0 2018-09-06 stsp
659 a440fac0 2018-09-06 stsp while (remain > 0) {
660 a440fac0 2018-09-06 stsp struct got_tree_entry *te;
661 a440fac0 2018-09-06 stsp size_t elen;
662 a440fac0 2018-09-06 stsp
663 a440fac0 2018-09-06 stsp err = parse_tree_entry(&te, &elen, buf, remain);
664 a440fac0 2018-09-06 stsp if (err)
665 a440fac0 2018-09-06 stsp return err;
666 a440fac0 2018-09-06 stsp (*tree)->entries.nentries++;
667 a440fac0 2018-09-06 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
668 a440fac0 2018-09-06 stsp buf += elen;
669 a440fac0 2018-09-06 stsp remain -= elen;
670 a440fac0 2018-09-06 stsp }
671 a440fac0 2018-09-06 stsp
672 a440fac0 2018-09-06 stsp if (remain != 0) {
673 a440fac0 2018-09-06 stsp got_object_tree_close(*tree);
674 a440fac0 2018-09-06 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
675 a440fac0 2018-09-06 stsp }
676 a440fac0 2018-09-06 stsp
677 a440fac0 2018-09-06 stsp return NULL;
678 a440fac0 2018-09-06 stsp }
679 a440fac0 2018-09-06 stsp
680 ad242220 2018-09-08 stsp const struct got_error *
681 ad242220 2018-09-08 stsp got_read_file_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
682 a440fac0 2018-09-06 stsp {
683 a440fac0 2018-09-06 stsp const struct got_error *err = NULL;
684 a440fac0 2018-09-06 stsp static const size_t blocksize = 512;
685 a440fac0 2018-09-06 stsp size_t n, total, remain;
686 a440fac0 2018-09-06 stsp uint8_t *buf;
687 a440fac0 2018-09-06 stsp
688 a440fac0 2018-09-06 stsp *outbuf = NULL;
689 a440fac0 2018-09-06 stsp *outlen = 0;
690 a440fac0 2018-09-06 stsp
691 a440fac0 2018-09-06 stsp buf = malloc(blocksize);
692 a440fac0 2018-09-06 stsp if (buf == NULL)
693 a440fac0 2018-09-06 stsp return got_error_from_errno();
694 a440fac0 2018-09-06 stsp
695 a440fac0 2018-09-06 stsp remain = blocksize;
696 a440fac0 2018-09-06 stsp total = 0;
697 a440fac0 2018-09-06 stsp while (1) {
698 a440fac0 2018-09-06 stsp if (remain == 0) {
699 a440fac0 2018-09-06 stsp uint8_t *newbuf;
700 a440fac0 2018-09-06 stsp newbuf = reallocarray(buf, 1, total + blocksize);
701 a440fac0 2018-09-06 stsp if (newbuf == NULL) {
702 a440fac0 2018-09-06 stsp err = got_error_from_errno();
703 a440fac0 2018-09-06 stsp goto done;
704 a440fac0 2018-09-06 stsp }
705 a440fac0 2018-09-06 stsp buf = newbuf;
706 a440fac0 2018-09-06 stsp remain += blocksize;
707 a440fac0 2018-09-06 stsp }
708 a440fac0 2018-09-06 stsp n = fread(buf + total, 1, remain, f);
709 a440fac0 2018-09-06 stsp if (n == 0) {
710 a440fac0 2018-09-06 stsp if (ferror(f)) {
711 a440fac0 2018-09-06 stsp err = got_ferror(f, GOT_ERR_IO);
712 a440fac0 2018-09-06 stsp goto done;
713 a440fac0 2018-09-06 stsp }
714 a440fac0 2018-09-06 stsp break; /* EOF */
715 a440fac0 2018-09-06 stsp }
716 a440fac0 2018-09-06 stsp remain -= n;
717 a440fac0 2018-09-06 stsp total += n;
718 a440fac0 2018-09-06 stsp };
719 a440fac0 2018-09-06 stsp
720 a440fac0 2018-09-06 stsp done:
721 a440fac0 2018-09-06 stsp if (err == NULL) {
722 a440fac0 2018-09-06 stsp *outbuf = buf;
723 a440fac0 2018-09-06 stsp *outlen = total;
724 a440fac0 2018-09-06 stsp } else
725 a440fac0 2018-09-06 stsp free(buf);
726 ad242220 2018-09-08 stsp return err;
727 a440fac0 2018-09-06 stsp }