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 be37c2e6 2018-04-24 stsp setproctitle("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 b419fc47 2018-04-26 stsp static const struct got_error *
265 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
267 b419fc47 2018-04-26 stsp int child_status;
269 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
271 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
272 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
274 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
275 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
277 b419fc47 2018-04-26 stsp return NULL;
280 2178c42e 2018-04-22 stsp static const struct got_error *
281 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
283 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
284 2178c42e 2018-04-22 stsp int imsg_fds[2];
285 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
288 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
289 2178c42e 2018-04-22 stsp return got_error_from_errno();
291 2178c42e 2018-04-22 stsp pid = fork();
292 2178c42e 2018-04-22 stsp if (pid == -1)
293 2178c42e 2018-04-22 stsp return got_error_from_errno();
294 2178c42e 2018-04-22 stsp else if (pid == 0) {
295 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
296 302b7dd6 2018-04-23 stsp /* not reached */
299 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
300 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
301 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
302 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
303 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
304 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
305 b419fc47 2018-04-26 stsp return err ? err : err_child;
308 d1cda826 2017-11-06 stsp static const struct got_error *
309 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
311 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
313 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
315 e6b1056e 2018-04-22 stsp *path = NULL;
317 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
318 0a585a0d 2018-03-17 stsp return got_error_from_errno();
320 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
322 ef0981d5 2018-02-12 stsp return err;
324 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
325 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
326 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
329 d1cda826 2017-11-06 stsp free(path_objects);
330 d1cda826 2017-11-06 stsp return err;
333 4ee4114f 2018-01-23 stsp static const struct got_error *
334 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
336 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
337 a1fd68d8 2018-01-12 stsp char *path;
339 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
341 d1cda826 2017-11-06 stsp return err;
342 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
343 d5003b79 2018-04-22 stsp if (*fd == -1) {
344 6c00b545 2018-01-17 stsp err = got_error_from_errno();
348 4558fcd4 2018-01-14 stsp free(path);
349 4558fcd4 2018-01-14 stsp return err;
352 4558fcd4 2018-01-14 stsp const struct got_error *
353 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
354 4558fcd4 2018-01-14 stsp struct got_object_id *id)
356 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
357 6c00b545 2018-01-17 stsp char *path;
360 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
362 4558fcd4 2018-01-14 stsp return err;
364 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
365 2178c42e 2018-04-22 stsp if (fd == -1) {
366 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
367 6c00b545 2018-01-17 stsp err = got_error_from_errno();
370 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
373 6c00b545 2018-01-17 stsp if (*obj == NULL)
374 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
376 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
379 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
382 6c00b545 2018-01-17 stsp free(path);
383 2178c42e 2018-04-22 stsp if (fd != -1)
385 ab9a70b2 2017-11-06 stsp return err;
389 6dfa2fd3 2018-02-12 stsp const struct got_error *
390 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
391 6dfa2fd3 2018-02-12 stsp const char *id_str)
393 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
395 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
396 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
398 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
402 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
404 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
405 c3703302 2018-01-23 stsp struct got_delta *delta;
406 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
407 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
408 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
409 c3703302 2018-01-23 stsp got_delta_close(delta);
412 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
413 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
417 bff6ca00 2018-04-23 stsp struct got_commit_object *
418 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
420 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
422 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
423 bff6ca00 2018-04-23 stsp if (commit == NULL)
424 bff6ca00 2018-04-23 stsp return NULL;
425 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
426 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
427 bff6ca00 2018-04-23 stsp free(commit);
428 bff6ca00 2018-04-23 stsp return NULL;
431 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
433 bff6ca00 2018-04-23 stsp return commit;
436 bff6ca00 2018-04-23 stsp const struct got_error *
437 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
438 bff6ca00 2018-04-23 stsp const char *id_str)
440 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
441 bff6ca00 2018-04-23 stsp struct got_parent_id *pid;
443 bff6ca00 2018-04-23 stsp pid = calloc(1, sizeof(*pid));
444 bff6ca00 2018-04-23 stsp if (pid == NULL)
445 bff6ca00 2018-04-23 stsp return got_error_from_errno();
447 bff6ca00 2018-04-23 stsp pid->id = calloc(1, sizeof(*pid->id));
448 bff6ca00 2018-04-23 stsp if (pid->id == NULL) {
449 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
451 bff6ca00 2018-04-23 stsp return err;
454 bff6ca00 2018-04-23 stsp if (!got_parse_sha1_digest(pid->id->sha1, id_str)) {
455 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
456 bff6ca00 2018-04-23 stsp free(pid->id);
458 bff6ca00 2018-04-23 stsp return err;
461 bff6ca00 2018-04-23 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, pid, entry);
462 bff6ca00 2018-04-23 stsp commit->nparents++;
464 bff6ca00 2018-04-23 stsp return NULL;
467 d1cda826 2017-11-06 stsp static const struct got_error *
468 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
470 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
471 d1cda826 2017-11-06 stsp char *s = buf;
472 d1cda826 2017-11-06 stsp size_t tlen;
473 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
475 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
476 d1cda826 2017-11-06 stsp if (*commit == NULL)
477 0a585a0d 2018-03-17 stsp return got_error_from_errno();
479 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
480 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
481 d1cda826 2017-11-06 stsp remain -= tlen;
482 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
483 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
487 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
488 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
491 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
492 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
494 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
498 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
499 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
500 d1cda826 2017-11-06 stsp remain -= tlen;
501 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
502 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
506 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
510 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
511 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
514 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
515 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
518 d1cda826 2017-11-06 stsp remain -= tlen;
519 d1cda826 2017-11-06 stsp if (remain <= 0) {
520 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
524 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
525 d1cda826 2017-11-06 stsp if (p == NULL) {
526 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
530 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
531 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
532 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
535 d1cda826 2017-11-06 stsp s += strlen((*commit)->author) + 1;
536 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->author) + 1;
539 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
540 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
543 d1cda826 2017-11-06 stsp remain -= tlen;
544 d1cda826 2017-11-06 stsp if (remain <= 0) {
545 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
549 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
550 d1cda826 2017-11-06 stsp if (p == NULL) {
551 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
555 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
556 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
557 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
560 d1cda826 2017-11-06 stsp s += strlen((*commit)->committer) + 1;
561 eb651edf 2018-02-11 stsp remain -= strlen((*commit)->committer) + 1;
564 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
565 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
566 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
571 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
572 59ece79d 2018-02-12 stsp *commit = NULL;
574 0ffeb3c2 2017-11-26 stsp return err;
577 0ffeb3c2 2017-11-26 stsp static void
578 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
580 59ece79d 2018-02-12 stsp free(te->id);
581 0ffeb3c2 2017-11-26 stsp free(te->name);
585 e033d803 2018-04-23 stsp struct got_tree_entry *
586 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
588 e033d803 2018-04-23 stsp struct got_tree_entry *te;
590 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
591 e033d803 2018-04-23 stsp if (te == NULL)
592 e033d803 2018-04-23 stsp return NULL;
594 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
595 e033d803 2018-04-23 stsp if (te->id == NULL) {
602 0ffeb3c2 2017-11-26 stsp static const struct got_error *
603 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
604 0ffeb3c2 2017-11-26 stsp size_t maxlen)
606 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
607 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
609 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
610 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
611 0a585a0d 2018-03-17 stsp return got_error_from_errno();
613 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
614 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
616 59ece79d 2018-02-12 stsp *te = NULL;
617 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
620 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
621 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
622 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
624 59ece79d 2018-02-12 stsp *te = NULL;
625 0a585a0d 2018-03-17 stsp return err;
627 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
628 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
629 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
633 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
637 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
638 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
639 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
642 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
643 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
644 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
647 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
648 59ece79d 2018-02-12 stsp *te = NULL;
650 d1cda826 2017-11-06 stsp return err;
653 d1cda826 2017-11-06 stsp static const struct got_error *
654 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
656 90356acc 2018-01-27 stsp const struct got_error *err;
657 0ffeb3c2 2017-11-26 stsp size_t remain = len;
659 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
660 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
661 0a585a0d 2018-03-17 stsp return got_error_from_errno();
663 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
665 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
666 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
667 0ffeb3c2 2017-11-26 stsp size_t elen;
669 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
671 90356acc 2018-01-27 stsp return err;
672 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
673 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
674 0ffeb3c2 2017-11-26 stsp buf += elen;
675 0ffeb3c2 2017-11-26 stsp remain -= elen;
678 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
679 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
680 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
683 0ffeb3c2 2017-11-26 stsp return NULL;
686 0ffeb3c2 2017-11-26 stsp static const struct got_error *
687 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
689 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
690 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
691 eb651edf 2018-02-11 stsp size_t n, total, remain;
692 eb651edf 2018-02-11 stsp uint8_t *buf;
694 eb651edf 2018-02-11 stsp *outbuf = NULL;
695 eb651edf 2018-02-11 stsp *outlen = 0;
697 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
698 eb651edf 2018-02-11 stsp if (buf == NULL)
699 0a585a0d 2018-03-17 stsp return got_error_from_errno();
701 eb651edf 2018-02-11 stsp remain = blocksize;
703 eb651edf 2018-02-11 stsp while (1) {
704 eb651edf 2018-02-11 stsp if (remain == 0) {
705 eb651edf 2018-02-11 stsp uint8_t *newbuf;
706 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
707 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
708 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
711 eb651edf 2018-02-11 stsp buf = newbuf;
712 eb651edf 2018-02-11 stsp remain += blocksize;
714 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
715 eb651edf 2018-02-11 stsp if (n == 0) {
716 eb651edf 2018-02-11 stsp if (ferror(f)) {
717 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
720 eb651edf 2018-02-11 stsp break; /* EOF */
722 eb651edf 2018-02-11 stsp remain -= n;
723 eb651edf 2018-02-11 stsp total += n;
727 eb651edf 2018-02-11 stsp if (err == NULL) {
728 eb651edf 2018-02-11 stsp *outbuf = buf;
729 eb651edf 2018-02-11 stsp *outlen = total;
732 eb651edf 2018-02-11 stsp return err;
735 eb651edf 2018-02-11 stsp static const struct got_error *
736 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
739 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
740 d1cda826 2017-11-06 stsp size_t len;
741 eb651edf 2018-02-11 stsp uint8_t *p;
743 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
744 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
746 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
748 d1cda826 2017-11-06 stsp return err;
750 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
751 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
755 d1cda826 2017-11-06 stsp /* Skip object header. */
756 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
757 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
760 d1cda826 2017-11-06 stsp return err;
763 bff6ca00 2018-04-23 stsp static void
764 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
765 bff6ca00 2018-04-23 stsp int imsg_fds[2])
767 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
768 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
769 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
770 bff6ca00 2018-04-23 stsp FILE *f = NULL;
771 bff6ca00 2018-04-23 stsp int status = 0;
773 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
774 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
775 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
777 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
778 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
779 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
783 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
784 bff6ca00 2018-04-23 stsp if (f == NULL) {
785 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
786 bff6ca00 2018-04-23 stsp close(obj_fd);
790 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
794 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
796 bff6ca00 2018-04-23 stsp if (commit)
797 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
799 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
800 bff6ca00 2018-04-23 stsp status = 1;
804 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
805 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
806 bff6ca00 2018-04-23 stsp _exit(status);
809 bff6ca00 2018-04-23 stsp static const struct got_error *
810 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
811 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
813 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
814 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
815 bff6ca00 2018-04-23 stsp int imsg_fds[2];
818 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
819 bff6ca00 2018-04-23 stsp return got_error_from_errno();
821 bff6ca00 2018-04-23 stsp pid = fork();
822 bff6ca00 2018-04-23 stsp if (pid == -1)
823 bff6ca00 2018-04-23 stsp return got_error_from_errno();
824 bff6ca00 2018-04-23 stsp else if (pid == 0) {
825 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
826 e506bf32 2018-04-23 stsp /* not reached */
829 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
830 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
831 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
832 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
833 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
834 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
835 b419fc47 2018-04-26 stsp return err ? err : err_child;
838 d1cda826 2017-11-06 stsp const struct got_error *
839 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
840 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
842 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
844 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
845 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
847 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
848 ea35256b 2018-03-16 stsp uint8_t *buf;
849 ea35256b 2018-03-16 stsp size_t len;
850 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
852 ea35256b 2018-03-16 stsp return err;
853 b29656e2 2018-03-16 stsp obj->size = len;
854 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
858 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
860 ea35256b 2018-03-16 stsp return err;
861 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
864 d1cda826 2017-11-06 stsp return err;
868 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
870 d1cda826 2017-11-06 stsp struct got_parent_id *pid;
872 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
873 d1cda826 2017-11-06 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
874 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
875 59ece79d 2018-02-12 stsp free(pid->id);
879 59ece79d 2018-02-12 stsp free(commit->tree_id);
880 d1cda826 2017-11-06 stsp free(commit->author);
881 d1cda826 2017-11-06 stsp free(commit->committer);
882 d1cda826 2017-11-06 stsp free(commit->logmsg);
883 d1cda826 2017-11-06 stsp free(commit);
886 0ffeb3c2 2017-11-26 stsp static const struct got_error *
887 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
889 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
890 0ffeb3c2 2017-11-26 stsp size_t len;
891 eb651edf 2018-02-11 stsp uint8_t *p;
893 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
894 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
896 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
898 0ffeb3c2 2017-11-26 stsp return err;
900 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
901 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
905 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
906 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
907 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
910 e033d803 2018-04-23 stsp return err;
913 e033d803 2018-04-23 stsp static void
914 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
915 e033d803 2018-04-23 stsp int imsg_fds[2])
917 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
918 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
919 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
920 e033d803 2018-04-23 stsp FILE *f = NULL;
921 e033d803 2018-04-23 stsp int status = 0;
923 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
924 e033d803 2018-04-23 stsp close(imsg_fds[0]);
925 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
927 e033d803 2018-04-23 stsp /* revoke access to most system calls */
928 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
929 e033d803 2018-04-23 stsp err = got_error_from_errno();
933 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
934 e033d803 2018-04-23 stsp if (f == NULL) {
935 e033d803 2018-04-23 stsp err = got_error_from_errno();
936 e033d803 2018-04-23 stsp close(obj_fd);
940 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
944 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
947 e033d803 2018-04-23 stsp got_object_tree_close(tree);
949 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
950 e033d803 2018-04-23 stsp status = 1;
954 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
955 e033d803 2018-04-23 stsp close(imsg_fds[1]);
956 e033d803 2018-04-23 stsp _exit(status);
959 e033d803 2018-04-23 stsp static const struct got_error *
960 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
963 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
964 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
965 e033d803 2018-04-23 stsp int imsg_fds[2];
968 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
969 e033d803 2018-04-23 stsp return got_error_from_errno();
971 e033d803 2018-04-23 stsp pid = fork();
972 e033d803 2018-04-23 stsp if (pid == -1)
973 e033d803 2018-04-23 stsp return got_error_from_errno();
974 e033d803 2018-04-23 stsp else if (pid == 0) {
975 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
976 e033d803 2018-04-23 stsp /* not reached */
979 e033d803 2018-04-23 stsp close(imsg_fds[1]);
980 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
981 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
982 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
983 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
984 e033d803 2018-04-23 stsp close(imsg_fds[0]);
985 b419fc47 2018-04-26 stsp return err ? err : err_child;
988 0ffeb3c2 2017-11-26 stsp const struct got_error *
989 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
990 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
992 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
994 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
995 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
997 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
998 e0ab43e7 2018-03-16 stsp uint8_t *buf;
999 e0ab43e7 2018-03-16 stsp size_t len;
1000 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1002 e0ab43e7 2018-03-16 stsp return err;
1003 b29656e2 2018-03-16 stsp obj->size = len;
1004 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1005 e0ab43e7 2018-03-16 stsp free(buf);
1008 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1010 e0ab43e7 2018-03-16 stsp return err;
1011 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1012 d5003b79 2018-04-22 stsp close(fd);
1014 0ffeb3c2 2017-11-26 stsp return err;
1018 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1020 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1022 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1023 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1024 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1025 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1028 f715ca7f 2017-11-27 stsp free(tree);
1031 ff6b18f8 2018-04-24 stsp static const struct got_error *
1032 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1034 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1035 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1036 ff6b18f8 2018-04-24 stsp int status = 0;
1037 2967a784 2018-04-24 stsp size_t size;
1038 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1040 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1041 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1042 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1044 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1045 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1046 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1047 ff6b18f8 2018-04-24 stsp goto done;
1050 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1051 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1052 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1053 8b2180d4 2018-04-26 stsp close(infd);
1054 8b2180d4 2018-04-26 stsp goto done;
1056 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1057 8b2180d4 2018-04-26 stsp fclose(infile);
1059 ff6b18f8 2018-04-24 stsp goto done;
1061 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1063 ff6b18f8 2018-04-24 stsp if (err) {
1064 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1065 ff6b18f8 2018-04-24 stsp status = 1;
1067 ff6b18f8 2018-04-24 stsp close(outfd);
1068 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1069 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1070 ff6b18f8 2018-04-24 stsp _exit(status);
1073 ff6b18f8 2018-04-24 stsp static const struct got_error *
1074 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1076 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1077 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1078 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1079 ff6b18f8 2018-04-24 stsp pid_t pid;
1081 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1082 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1084 ff6b18f8 2018-04-24 stsp pid = fork();
1085 ff6b18f8 2018-04-24 stsp if (pid == -1)
1086 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1087 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1088 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1089 ff6b18f8 2018-04-24 stsp /* not reached */
1092 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1093 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1094 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1095 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1096 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1097 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1098 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1099 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1100 b419fc47 2018-04-26 stsp return err ? err : err_child;
1103 68482ea3 2017-11-27 stsp const struct got_error *
1104 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1105 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1107 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1109 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1110 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1112 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1113 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1115 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1116 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1117 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1119 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1120 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1121 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1122 c7254d79 2018-04-24 stsp goto done;
1124 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1125 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1127 c7254d79 2018-04-24 stsp goto done;
1129 3aca5731 2018-04-24 stsp int infd, outfd;
1130 2967a784 2018-04-24 stsp size_t size;
1131 2967a784 2018-04-24 stsp struct stat sb;
1133 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1135 c7254d79 2018-04-24 stsp goto done;
1138 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1139 3aca5731 2018-04-24 stsp if (outfd == -1) {
1140 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1141 3aca5731 2018-04-24 stsp close(infd);
1142 3aca5731 2018-04-24 stsp goto done;
1145 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1146 3aca5731 2018-04-24 stsp close(infd);
1148 c7254d79 2018-04-24 stsp goto done;
1150 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1151 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1152 2967a784 2018-04-24 stsp close(outfd);
1153 2967a784 2018-04-24 stsp goto done;
1156 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1157 2967a784 2018-04-24 stsp err = got_error_from_errno();
1158 2967a784 2018-04-24 stsp close(outfd);
1159 2967a784 2018-04-24 stsp goto done;
1162 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1163 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1164 2967a784 2018-04-24 stsp close(outfd);
1165 2967a784 2018-04-24 stsp goto done;
1168 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1169 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1170 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1171 3aca5731 2018-04-24 stsp close(outfd);
1172 3aca5731 2018-04-24 stsp goto done;
1176 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1177 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1178 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1181 c7254d79 2018-04-24 stsp if (err && *blob) {
1182 c7254d79 2018-04-24 stsp if ((*blob)->f)
1183 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1184 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1185 c7254d79 2018-04-24 stsp free(*blob);
1186 c7254d79 2018-04-24 stsp *blob = NULL;
1188 68482ea3 2017-11-27 stsp return err;
1192 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1194 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1195 68482ea3 2017-11-27 stsp fclose(blob->f);
1196 68482ea3 2017-11-27 stsp free(blob);
1200 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1202 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1206 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1208 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1211 f934cf2c 2018-02-12 stsp const uint8_t *
1212 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1214 f934cf2c 2018-02-12 stsp return blob->read_buf;
1217 68482ea3 2017-11-27 stsp const struct got_error *
1218 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1222 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1223 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1224 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1225 eb651edf 2018-02-11 stsp *outlenp = n;
1226 eb651edf 2018-02-11 stsp return NULL;