2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
24 a1fd68d8 2018-01-12 stsp #include <errno.h>
25 2178c42e 2018-04-22 stsp #include <fcntl.h>
26 d71d75ad 2017-11-05 stsp #include <stdio.h>
27 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
28 ab9a70b2 2017-11-06 stsp #include <string.h>
29 2178c42e 2018-04-22 stsp #include <stdint.h>
30 d71d75ad 2017-11-05 stsp #include <sha1.h>
31 ab9a70b2 2017-11-06 stsp #include <zlib.h>
32 ab9a70b2 2017-11-06 stsp #include <ctype.h>
33 ab9a70b2 2017-11-06 stsp #include <limits.h>
34 2178c42e 2018-04-22 stsp #include <imsg.h>
36 ab9a70b2 2017-11-06 stsp #include "got_error.h"
37 d71d75ad 2017-11-05 stsp #include "got_object.h"
38 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
43 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
49 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
52 ab9a70b2 2017-11-06 stsp #ifndef nitems
53 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
56 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
57 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
58 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
60 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
61 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
62 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
65 ef0981d5 2018-02-12 stsp const struct got_error *
66 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
68 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
70 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
71 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
72 0a585a0d 2018-03-17 stsp return got_error_from_errno();
74 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
75 ef0981d5 2018-02-12 stsp free(*outbuf);
76 ef0981d5 2018-02-12 stsp *outbuf = NULL;
77 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
80 ef0981d5 2018-02-12 stsp return NULL;
84 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
86 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
89 8bf5b3c9 2018-03-17 stsp struct got_object_id *
90 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
92 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
94 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
95 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
96 8bf5b3c9 2018-03-17 stsp return NULL;
97 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
101 3235492e 2018-04-01 stsp struct got_object_id *
102 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
104 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
108 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
110 b107e67f 2018-01-19 stsp switch (obj->type) {
111 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
112 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
113 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
114 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
115 b107e67f 2018-01-19 stsp return obj->type;
121 96f5e8b3 2018-01-23 stsp /* not reached */
125 ab9a70b2 2017-11-06 stsp static const struct got_error *
126 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
128 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
129 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
130 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
131 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
133 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
134 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
135 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
136 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
138 ab9a70b2 2017-11-06 stsp int type = 0;
139 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
141 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
143 ab9a70b2 2017-11-06 stsp if (p == NULL)
144 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
146 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
148 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
149 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
150 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
151 ab9a70b2 2017-11-06 stsp const char *errstr;
153 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
156 ab9a70b2 2017-11-06 stsp type = obj_types[i];
157 63323519 2017-11-06 stsp if (len <= tlen)
158 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
159 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
160 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
161 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
165 ab9a70b2 2017-11-06 stsp if (type == 0)
166 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
168 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
169 1db76ab5 2018-01-26 mpi if (*obj == NULL)
170 0a585a0d 2018-03-17 stsp return got_error_from_errno();
171 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
172 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
173 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
174 ab9a70b2 2017-11-06 stsp return NULL;
177 ab9a70b2 2017-11-06 stsp static const struct got_error *
178 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
180 ab9a70b2 2017-11-06 stsp const struct got_error *err;
181 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
183 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
184 744d9326 2017-12-01 stsp size_t outlen, totlen;
187 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
188 a3e2cbea 2017-12-01 stsp if (buf == NULL)
189 0a585a0d 2018-03-17 stsp return got_error_from_errno();
191 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
193 ab9a70b2 2017-11-06 stsp return err;
196 744d9326 2017-12-01 stsp totlen = 0;
198 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
201 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
202 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
203 e302c59e 2017-12-01 stsp if (buf == NULL) {
204 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
208 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
209 744d9326 2017-12-01 stsp totlen += outlen;
211 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
213 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
215 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
216 2178c42e 2018-04-22 stsp return err;
219 302b7dd6 2018-04-23 stsp static void
220 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
222 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
223 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
224 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
225 302b7dd6 2018-04-23 stsp FILE *f = NULL;
226 302b7dd6 2018-04-23 stsp int status = 0;
228 302b7dd6 2018-04-23 stsp setproctitle("got: read object header");
229 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
230 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
232 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
233 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
234 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
238 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
239 302b7dd6 2018-04-23 stsp if (f == NULL) {
240 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
241 302b7dd6 2018-04-23 stsp close(obj_fd);
245 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
249 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
252 e3306bd9 2018-04-23 stsp got_object_close(obj);
254 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
255 302b7dd6 2018-04-23 stsp status = 1;
259 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
260 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
261 302b7dd6 2018-04-23 stsp _exit(status);
264 2178c42e 2018-04-22 stsp static const struct got_error *
265 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
267 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
268 2178c42e 2018-04-22 stsp int imsg_fds[2];
269 2178c42e 2018-04-22 stsp const struct got_error *err = NULL;
271 2178c42e 2018-04-22 stsp int child_status;
273 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
274 2178c42e 2018-04-22 stsp return got_error_from_errno();
276 2178c42e 2018-04-22 stsp pid = fork();
277 2178c42e 2018-04-22 stsp if (pid == -1)
278 2178c42e 2018-04-22 stsp return got_error_from_errno();
279 2178c42e 2018-04-22 stsp else if (pid == 0) {
280 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
281 302b7dd6 2018-04-23 stsp /* not reached */
284 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
285 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
286 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
287 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
288 2178c42e 2018-04-22 stsp waitpid(pid, &child_status, 0);
289 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
290 ab9a70b2 2017-11-06 stsp return err;
293 d1cda826 2017-11-06 stsp static const struct got_error *
294 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
296 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
298 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
300 e6b1056e 2018-04-22 stsp *path = NULL;
302 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
303 0a585a0d 2018-03-17 stsp return got_error_from_errno();
305 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
307 ef0981d5 2018-02-12 stsp return err;
309 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
310 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
311 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
314 d1cda826 2017-11-06 stsp free(path_objects);
315 d1cda826 2017-11-06 stsp return err;
318 4ee4114f 2018-01-23 stsp static const struct got_error *
319 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
321 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
322 a1fd68d8 2018-01-12 stsp char *path;
324 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
326 d1cda826 2017-11-06 stsp return err;
327 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
328 d5003b79 2018-04-22 stsp if (*fd == -1) {
329 6c00b545 2018-01-17 stsp err = got_error_from_errno();
333 4558fcd4 2018-01-14 stsp free(path);
334 4558fcd4 2018-01-14 stsp return err;
337 4558fcd4 2018-01-14 stsp const struct got_error *
338 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
339 4558fcd4 2018-01-14 stsp struct got_object_id *id)
341 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
342 6c00b545 2018-01-17 stsp char *path;
345 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
347 4558fcd4 2018-01-14 stsp return err;
349 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 2178c42e 2018-04-22 stsp if (fd == -1) {
351 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
352 6c00b545 2018-01-17 stsp err = got_error_from_errno();
355 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
358 6c00b545 2018-01-17 stsp if (*obj == NULL)
359 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
361 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
364 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
367 6c00b545 2018-01-17 stsp free(path);
368 2178c42e 2018-04-22 stsp if (fd != -1)
370 ab9a70b2 2017-11-06 stsp return err;
374 6dfa2fd3 2018-02-12 stsp const struct got_error *
375 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
376 6dfa2fd3 2018-02-12 stsp const char *id_str)
378 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
380 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
381 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
383 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
387 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
389 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
390 c3703302 2018-01-23 stsp struct got_delta *delta;
391 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
392 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
393 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
394 c3703302 2018-01-23 stsp got_delta_close(delta);
397 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
398 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
402 bff6ca00 2018-04-23 stsp struct got_commit_object *
403 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
405 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
407 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
408 bff6ca00 2018-04-23 stsp if (commit == NULL)
409 bff6ca00 2018-04-23 stsp return NULL;
410 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
411 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
412 bff6ca00 2018-04-23 stsp free(commit);
413 bff6ca00 2018-04-23 stsp return NULL;
416 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
418 bff6ca00 2018-04-23 stsp return commit;
421 bff6ca00 2018-04-23 stsp const struct got_error *
422 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
423 bff6ca00 2018-04-23 stsp const char *id_str)
425 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
426 bff6ca00 2018-04-23 stsp struct got_parent_id *pid;
428 bff6ca00 2018-04-23 stsp pid = calloc(1, sizeof(*pid));
429 bff6ca00 2018-04-23 stsp if (pid == NULL)
430 bff6ca00 2018-04-23 stsp return got_error_from_errno();
432 bff6ca00 2018-04-23 stsp pid->id = calloc(1, sizeof(*pid->id));
433 bff6ca00 2018-04-23 stsp if (pid->id == NULL) {
434 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
436 bff6ca00 2018-04-23 stsp return err;
439 bff6ca00 2018-04-23 stsp if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
440 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
441 bff6ca00 2018-04-23 stsp free(pid->id);
443 bff6ca00 2018-04-23 stsp return err;
446 bff6ca00 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
447 bff6ca00 2018-04-23 stsp commit->nparents++;
449 bff6ca00 2018-04-23 stsp return NULL;
452 d1cda826 2017-11-06 stsp static const struct got_error *
453 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
455 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
456 d1cda826 2017-11-06 stsp char *s = buf;
457 d1cda826 2017-11-06 stsp size_t tlen;
458 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
460 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
461 d1cda826 2017-11-06 stsp if (*commit == NULL)
462 0a585a0d 2018-03-17 stsp return got_error_from_errno();
464 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
465 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
466 d1cda826 2017-11-06 stsp remain -= tlen;
467 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
468 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
472 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
473 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
476 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
477 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
479 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
483 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
484 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
485 d1cda826 2017-11-06 stsp remain -= tlen;
486 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
487 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
495 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
496 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
499 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
500 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
503 d1cda826 2017-11-06 stsp remain -= tlen;
504 d1cda826 2017-11-06 stsp if (remain <= 0) {
505 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
509 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
510 d1cda826 2017-11-06 stsp if (p == NULL) {
511 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
515 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
516 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
517 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
520 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
521 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
524 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
525 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
528 d1cda826 2017-11-06 stsp remain -= tlen;
529 d1cda826 2017-11-06 stsp if (remain <= 0) {
530 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
534 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
535 d1cda826 2017-11-06 stsp if (p == NULL) {
536 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
540 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
541 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
542 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
545 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
546 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
549 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
550 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
551 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
556 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
557 59ece79d 2018-02-12 stsp *commit = NULL;
559 0ffeb3c2 2017-11-26 stsp return err;
562 0ffeb3c2 2017-11-26 stsp static void
563 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
565 59ece79d 2018-02-12 stsp free(te->id);
566 0ffeb3c2 2017-11-26 stsp free(te->name);
570 0ffeb3c2 2017-11-26 stsp static const struct got_error *
571 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
572 0ffeb3c2 2017-11-26 stsp size_t maxlen)
574 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
575 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
577 0ffeb3c2 2017-11-26 stsp *te = calloc(1, sizeof(**te));
578 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
579 0a585a0d 2018-03-17 stsp return got_error_from_errno();
581 59ece79d 2018-02-12 stsp (*te)->id = calloc(1, sizeof(*(*te)->id));
582 59ece79d 2018-02-12 stsp if ((*te)->id == NULL) {
583 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
585 59ece79d 2018-02-12 stsp *te = NULL;
586 0a585a0d 2018-03-17 stsp return err;
589 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
590 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
592 59ece79d 2018-02-12 stsp *te = NULL;
593 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
596 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
597 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
598 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
600 59ece79d 2018-02-12 stsp *te = NULL;
601 0a585a0d 2018-03-17 stsp return err;
603 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
604 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
605 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
608 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
609 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
613 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
614 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
615 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
619 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
620 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
623 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
624 59ece79d 2018-02-12 stsp *te = NULL;
626 d1cda826 2017-11-06 stsp return err;
629 d1cda826 2017-11-06 stsp static const struct got_error *
630 0ffeb3c2 2017-11-26 stsp parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
631 e0ab43e7 2018-03-16 stsp uint8_t *buf, size_t len)
633 90356acc 2018-01-27 stsp const struct got_error *err;
634 0ffeb3c2 2017-11-26 stsp size_t remain = len;
636 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
637 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
638 0a585a0d 2018-03-17 stsp return got_error_from_errno();
640 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
642 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
643 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
644 0ffeb3c2 2017-11-26 stsp size_t elen;
646 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
648 90356acc 2018-01-27 stsp return err;
649 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
650 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
651 0ffeb3c2 2017-11-26 stsp buf += elen;
652 0ffeb3c2 2017-11-26 stsp remain -= elen;
655 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
656 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
657 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
660 0ffeb3c2 2017-11-26 stsp return NULL;
663 0ffeb3c2 2017-11-26 stsp static const struct got_error *
664 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
666 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
667 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
668 eb651edf 2018-02-11 stsp size_t n, total, remain;
669 eb651edf 2018-02-11 stsp uint8_t *buf;
671 eb651edf 2018-02-11 stsp *outbuf = NULL;
672 eb651edf 2018-02-11 stsp *outlen = 0;
674 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
675 eb651edf 2018-02-11 stsp if (buf == NULL)
676 0a585a0d 2018-03-17 stsp return got_error_from_errno();
678 eb651edf 2018-02-11 stsp remain = blocksize;
680 eb651edf 2018-02-11 stsp while (1) {
681 eb651edf 2018-02-11 stsp if (remain == 0) {
682 eb651edf 2018-02-11 stsp uint8_t *newbuf;
683 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
684 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
685 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
688 eb651edf 2018-02-11 stsp buf = newbuf;
689 eb651edf 2018-02-11 stsp remain += blocksize;
691 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
692 eb651edf 2018-02-11 stsp if (n == 0) {
693 eb651edf 2018-02-11 stsp if (ferror(f)) {
694 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
697 eb651edf 2018-02-11 stsp break; /* EOF */
699 eb651edf 2018-02-11 stsp remain -= n;
700 eb651edf 2018-02-11 stsp total += n;
704 eb651edf 2018-02-11 stsp if (err == NULL) {
705 eb651edf 2018-02-11 stsp *outbuf = buf;
706 eb651edf 2018-02-11 stsp *outlen = total;
709 eb651edf 2018-02-11 stsp return err;
712 eb651edf 2018-02-11 stsp static const struct got_error *
713 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
716 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
717 d1cda826 2017-11-06 stsp size_t len;
718 eb651edf 2018-02-11 stsp uint8_t *p;
720 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
721 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
723 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
725 d1cda826 2017-11-06 stsp return err;
727 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
728 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
732 d1cda826 2017-11-06 stsp /* Skip object header. */
733 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
734 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
737 d1cda826 2017-11-06 stsp return err;
740 bff6ca00 2018-04-23 stsp static void
741 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
742 bff6ca00 2018-04-23 stsp int imsg_fds[2])
744 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
745 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
746 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
747 bff6ca00 2018-04-23 stsp FILE *f = NULL;
748 bff6ca00 2018-04-23 stsp int status = 0;
750 bff6ca00 2018-04-23 stsp setproctitle("got: read commit object");
751 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
752 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
754 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
755 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
756 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
760 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
761 bff6ca00 2018-04-23 stsp if (f == NULL) {
762 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
763 bff6ca00 2018-04-23 stsp close(obj_fd);
767 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
771 bff6ca00 2018-04-23 stsp err = got_privsep_send_commit_obj(&ibuf, commit);
773 bff6ca00 2018-04-23 stsp if (commit)
774 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
776 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
777 bff6ca00 2018-04-23 stsp status = 1;
781 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
782 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
783 bff6ca00 2018-04-23 stsp _exit(status);
786 bff6ca00 2018-04-23 stsp static const struct got_error *
787 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
788 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
790 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
791 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
792 bff6ca00 2018-04-23 stsp int imsg_fds[2];
794 bff6ca00 2018-04-23 stsp int child_status;
796 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
797 bff6ca00 2018-04-23 stsp return got_error_from_errno();
799 bff6ca00 2018-04-23 stsp pid = fork();
800 bff6ca00 2018-04-23 stsp if (pid == -1)
801 bff6ca00 2018-04-23 stsp return got_error_from_errno();
802 bff6ca00 2018-04-23 stsp else if (pid == 0) {
803 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
804 e506bf32 2018-04-23 stsp /* not reached */
807 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
808 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
809 bff6ca00 2018-04-23 stsp err = got_privsep_recv_commit_obj(commit, &parent_ibuf);
810 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
811 bff6ca00 2018-04-23 stsp waitpid(pid, &child_status, 0);
812 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
813 bff6ca00 2018-04-23 stsp return err;
816 d1cda826 2017-11-06 stsp const struct got_error *
817 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
818 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
820 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
822 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
823 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
825 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
826 ea35256b 2018-03-16 stsp uint8_t *buf;
827 ea35256b 2018-03-16 stsp size_t len;
828 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
830 ea35256b 2018-03-16 stsp return err;
831 b29656e2 2018-03-16 stsp obj->size = len;
832 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
836 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
838 ea35256b 2018-03-16 stsp return err;
839 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
842 d1cda826 2017-11-06 stsp return err;
846 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
848 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
850 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
851 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
852 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
853 59ece79d 2018-02-12 stsp free(pid->id);
857 59ece79d 2018-02-12 stsp free(commit->tree_id);
858 d1cda826 2017-11-06 stsp free(commit->author);
859 d1cda826 2017-11-06 stsp free(commit->committer);
860 d1cda826 2017-11-06 stsp free(commit->logmsg);
861 d1cda826 2017-11-06 stsp free(commit);
864 0ffeb3c2 2017-11-26 stsp static const struct got_error *
865 0ffeb3c2 2017-11-26 stsp read_tree_object(struct got_tree_object **tree,
866 4558fcd4 2018-01-14 stsp struct got_repository *repo, struct got_object *obj, FILE *f)
868 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
869 0ffeb3c2 2017-11-26 stsp size_t len;
870 eb651edf 2018-02-11 stsp uint8_t *p;
872 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
873 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
875 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
877 0ffeb3c2 2017-11-26 stsp return err;
879 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
880 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
884 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
885 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
886 eb651edf 2018-02-11 stsp err = parse_tree_object(tree, repo, p + obj->hdrlen, len);
889 0ffeb3c2 2017-11-26 stsp return err;
892 0ffeb3c2 2017-11-26 stsp const struct got_error *
893 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
894 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
896 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
898 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
899 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
901 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
902 e0ab43e7 2018-03-16 stsp uint8_t *buf;
903 e0ab43e7 2018-03-16 stsp size_t len;
904 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
906 e0ab43e7 2018-03-16 stsp return err;
907 b29656e2 2018-03-16 stsp obj->size = len;
908 b29656e2 2018-03-16 stsp err = parse_tree_object(tree, repo, buf, len);
913 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
915 e0ab43e7 2018-03-16 stsp return err;
916 d5003b79 2018-04-22 stsp f = fdopen(fd, "rb");
917 d5003b79 2018-04-22 stsp if (f == NULL) {
919 d5003b79 2018-04-22 stsp return got_error_from_errno();
921 e0ab43e7 2018-03-16 stsp err = read_tree_object(tree, repo, obj, f);
925 0ffeb3c2 2017-11-26 stsp return err;
929 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
931 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
933 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
934 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
935 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
936 f715ca7f 2017-11-27 stsp tree_entry_close(te);
939 f715ca7f 2017-11-27 stsp free(tree);
942 68482ea3 2017-11-27 stsp const struct got_error *
943 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
944 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
946 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
948 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
949 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
951 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
952 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
954 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
955 4558fcd4 2018-01-14 stsp if (*blob == NULL)
956 0a585a0d 2018-03-17 stsp return got_error_from_errno();
958 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
959 eb651edf 2018-02-11 stsp (*blob)->read_buf = calloc(1, blocksize);
960 56866f4a 2018-03-17 stsp if ((*blob)->read_buf == NULL) {
961 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
962 56866f4a 2018-03-17 stsp free(*blob);
963 56866f4a 2018-03-17 stsp *blob = NULL;
964 0a585a0d 2018-03-17 stsp return err;
966 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
968 56866f4a 2018-03-17 stsp free((*blob)->read_buf);
969 56866f4a 2018-03-17 stsp free(*blob);
970 56866f4a 2018-03-17 stsp *blob = NULL;
971 eb651edf 2018-02-11 stsp return err;
975 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
977 eb651edf 2018-02-11 stsp free(*blob);
978 d0f3be7c 2018-03-17 stsp *blob = NULL;
979 eb651edf 2018-02-11 stsp return err;
981 d5003b79 2018-04-22 stsp (*blob)->f = fdopen(fd, "rb");
982 d5003b79 2018-04-22 stsp if ((*blob)->f == NULL) {
983 d5003b79 2018-04-22 stsp free(*blob);
984 d5003b79 2018-04-22 stsp *blob = NULL;
986 d5003b79 2018-04-22 stsp return err;
989 19d747f7 2018-03-16 stsp err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
990 eb651edf 2018-02-11 stsp if (err != NULL) {
991 eb651edf 2018-02-11 stsp fclose((*blob)->f);
992 eb651edf 2018-02-11 stsp free(*blob);
993 56866f4a 2018-03-17 stsp *blob = NULL;
994 eb651edf 2018-02-11 stsp return err;
997 eb651edf 2018-02-11 stsp (*blob)->read_buf = (*blob)->zb.outbuf;
998 eb651edf 2018-02-11 stsp (*blob)->flags |= GOT_BLOB_F_COMPRESSED;
1001 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1002 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1003 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1005 68482ea3 2017-11-27 stsp return err;
1009 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1011 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
1012 eb651edf 2018-02-11 stsp got_inflate_end(&blob->zb);
1014 eb651edf 2018-02-11 stsp free(blob->read_buf);
1015 68482ea3 2017-11-27 stsp fclose(blob->f);
1016 68482ea3 2017-11-27 stsp free(blob);
1020 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1022 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1026 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1028 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1031 f934cf2c 2018-02-12 stsp const uint8_t *
1032 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1034 f934cf2c 2018-02-12 stsp return blob->read_buf;
1037 68482ea3 2017-11-27 stsp const struct got_error *
1038 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1042 eb651edf 2018-02-11 stsp if (blob->flags & GOT_BLOB_F_COMPRESSED)
1043 eb651edf 2018-02-11 stsp return got_inflate_read(&blob->zb, blob->f, outlenp);
1045 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1046 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1047 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1048 eb651edf 2018-02-11 stsp *outlenp = n;
1049 eb651edf 2018-02-11 stsp return NULL;