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"
39 511a516b 2018-05-19 stsp #include "got_opentemp.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
44 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
47 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 ab9a70b2 2017-11-06 stsp #ifndef nitems
54 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
57 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
58 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
59 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
61 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
62 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
64 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
66 ef0981d5 2018-02-12 stsp const struct got_error *
67 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
69 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
71 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
72 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
73 0a585a0d 2018-03-17 stsp return got_error_from_errno();
75 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
76 ef0981d5 2018-02-12 stsp free(*outbuf);
77 ef0981d5 2018-02-12 stsp *outbuf = NULL;
78 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
81 ef0981d5 2018-02-12 stsp return NULL;
85 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
87 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
90 8bf5b3c9 2018-03-17 stsp struct got_object_id *
91 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
93 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
95 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
96 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
97 8bf5b3c9 2018-03-17 stsp return NULL;
98 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
102 3235492e 2018-04-01 stsp struct got_object_id *
103 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
105 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
108 bacc9935 2018-05-20 stsp const struct got_error *
109 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
111 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
115 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
117 b107e67f 2018-01-19 stsp switch (obj->type) {
118 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
119 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
120 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
121 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
122 b107e67f 2018-01-19 stsp return obj->type;
128 96f5e8b3 2018-01-23 stsp /* not reached */
132 ab9a70b2 2017-11-06 stsp static const struct got_error *
133 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
135 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
136 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
137 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
138 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
140 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
141 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
142 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
143 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
145 ab9a70b2 2017-11-06 stsp int type = 0;
146 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
148 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
150 ab9a70b2 2017-11-06 stsp if (p == NULL)
151 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
153 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
155 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
156 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
157 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
158 ab9a70b2 2017-11-06 stsp const char *errstr;
160 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
163 ab9a70b2 2017-11-06 stsp type = obj_types[i];
164 63323519 2017-11-06 stsp if (len <= tlen)
165 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
166 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
167 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
168 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
172 ab9a70b2 2017-11-06 stsp if (type == 0)
173 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
175 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
176 1db76ab5 2018-01-26 mpi if (*obj == NULL)
177 0a585a0d 2018-03-17 stsp return got_error_from_errno();
178 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
179 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
180 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
181 ab9a70b2 2017-11-06 stsp return NULL;
184 ab9a70b2 2017-11-06 stsp static const struct got_error *
185 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
187 ab9a70b2 2017-11-06 stsp const struct got_error *err;
188 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
190 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
191 744d9326 2017-12-01 stsp size_t outlen, totlen;
194 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
195 a3e2cbea 2017-12-01 stsp if (buf == NULL)
196 0a585a0d 2018-03-17 stsp return got_error_from_errno();
198 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
200 ab9a70b2 2017-11-06 stsp return err;
203 744d9326 2017-12-01 stsp totlen = 0;
205 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
208 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
209 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
210 e302c59e 2017-12-01 stsp if (buf == NULL) {
211 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
215 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
216 744d9326 2017-12-01 stsp totlen += outlen;
218 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
220 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
222 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
223 2178c42e 2018-04-22 stsp return err;
226 302b7dd6 2018-04-23 stsp static void
227 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
229 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
230 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
231 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
232 302b7dd6 2018-04-23 stsp FILE *f = NULL;
233 302b7dd6 2018-04-23 stsp int status = 0;
235 be37c2e6 2018-04-24 stsp setproctitle("read object header");
236 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
237 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
239 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
240 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
241 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
245 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
246 302b7dd6 2018-04-23 stsp if (f == NULL) {
247 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
248 302b7dd6 2018-04-23 stsp close(obj_fd);
252 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
256 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
259 e3306bd9 2018-04-23 stsp got_object_close(obj);
261 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
262 302b7dd6 2018-04-23 stsp status = 1;
266 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
267 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
268 302b7dd6 2018-04-23 stsp _exit(status);
271 b419fc47 2018-04-26 stsp static const struct got_error *
272 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
274 b419fc47 2018-04-26 stsp int child_status;
276 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
278 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
279 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
281 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
282 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
284 b419fc47 2018-04-26 stsp return NULL;
287 2178c42e 2018-04-22 stsp static const struct got_error *
288 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
290 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
291 2178c42e 2018-04-22 stsp int imsg_fds[2];
292 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
295 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
296 2178c42e 2018-04-22 stsp return got_error_from_errno();
298 2178c42e 2018-04-22 stsp pid = fork();
299 2178c42e 2018-04-22 stsp if (pid == -1)
300 2178c42e 2018-04-22 stsp return got_error_from_errno();
301 2178c42e 2018-04-22 stsp else if (pid == 0) {
302 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
303 302b7dd6 2018-04-23 stsp /* not reached */
306 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
307 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
308 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
309 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
310 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
311 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
312 b419fc47 2018-04-26 stsp return err ? err : err_child;
315 d1cda826 2017-11-06 stsp static const struct got_error *
316 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
318 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
320 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
322 e6b1056e 2018-04-22 stsp *path = NULL;
324 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
325 0a585a0d 2018-03-17 stsp return got_error_from_errno();
327 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
329 ef0981d5 2018-02-12 stsp return err;
331 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
332 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
333 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
336 d1cda826 2017-11-06 stsp free(path_objects);
337 d1cda826 2017-11-06 stsp return err;
340 4ee4114f 2018-01-23 stsp static const struct got_error *
341 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
343 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
344 a1fd68d8 2018-01-12 stsp char *path;
346 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
348 d1cda826 2017-11-06 stsp return err;
349 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
350 d5003b79 2018-04-22 stsp if (*fd == -1) {
351 6c00b545 2018-01-17 stsp err = got_error_from_errno();
355 4558fcd4 2018-01-14 stsp free(path);
356 4558fcd4 2018-01-14 stsp return err;
359 4558fcd4 2018-01-14 stsp const struct got_error *
360 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
361 4558fcd4 2018-01-14 stsp struct got_object_id *id)
363 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
364 6c00b545 2018-01-17 stsp char *path;
367 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
369 4558fcd4 2018-01-14 stsp return err;
371 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
372 2178c42e 2018-04-22 stsp if (fd == -1) {
373 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
374 6c00b545 2018-01-17 stsp err = got_error_from_errno();
377 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
380 6c00b545 2018-01-17 stsp if (*obj == NULL)
381 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
383 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
386 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
389 6c00b545 2018-01-17 stsp free(path);
390 2178c42e 2018-04-22 stsp if (fd != -1)
392 ab9a70b2 2017-11-06 stsp return err;
396 6dfa2fd3 2018-02-12 stsp const struct got_error *
397 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
398 6dfa2fd3 2018-02-12 stsp const char *id_str)
400 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
402 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
403 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
405 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
409 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
411 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
412 c3703302 2018-01-23 stsp struct got_delta *delta;
413 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
414 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
415 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
416 c3703302 2018-01-23 stsp got_delta_close(delta);
419 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
420 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
424 bff6ca00 2018-04-23 stsp struct got_commit_object *
425 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
427 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
429 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
430 bff6ca00 2018-04-23 stsp if (commit == NULL)
431 bff6ca00 2018-04-23 stsp return NULL;
432 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
433 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
434 bff6ca00 2018-04-23 stsp free(commit);
435 bff6ca00 2018-04-23 stsp return NULL;
438 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
440 bff6ca00 2018-04-23 stsp return commit;
443 bff6ca00 2018-04-23 stsp const struct got_error *
444 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
445 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
447 be6a1b5a 2018-06-11 stsp const struct got_error *err;
448 be6a1b5a 2018-06-11 stsp struct got_object *obj;
450 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
452 be6a1b5a 2018-06-11 stsp return err;
453 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
454 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
458 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
460 be6a1b5a 2018-06-11 stsp got_object_close(obj);
461 be6a1b5a 2018-06-11 stsp return err;
464 be6a1b5a 2018-06-11 stsp const struct got_error *
465 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
466 bff6ca00 2018-04-23 stsp const char *id_str)
468 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
469 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
471 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
472 79f35eb3 2018-06-11 stsp if (qid == NULL)
473 bff6ca00 2018-04-23 stsp return got_error_from_errno();
475 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
476 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
477 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
479 bff6ca00 2018-04-23 stsp return err;
482 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
483 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
484 79f35eb3 2018-06-11 stsp free(qid->id);
486 bff6ca00 2018-04-23 stsp return err;
489 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
490 bff6ca00 2018-04-23 stsp commit->nparents++;
492 4626e416 2018-06-10 stsp return NULL;
495 4626e416 2018-06-10 stsp static const struct got_error *
496 6c281f94 2018-06-11 stsp parse_commit_time(time_t *time, char **tzoff, char *committer)
498 4626e416 2018-06-10 stsp const char *errstr;
499 4626e416 2018-06-10 stsp char *space;
503 6c281f94 2018-06-11 stsp /* Parse and then strip trailing timezone indicator. */
504 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
505 4626e416 2018-06-10 stsp if (space == NULL)
506 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
507 6c281f94 2018-06-11 stsp *tzoff = strdup(space + 1);
508 6c281f94 2018-06-11 stsp if (*tzoff == NULL)
509 6c281f94 2018-06-11 stsp return got_error_from_errno();
510 4626e416 2018-06-10 stsp *space = '\0';
512 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
513 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
514 4626e416 2018-06-10 stsp if (space == NULL)
515 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
517 4626e416 2018-06-10 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
518 4626e416 2018-06-10 stsp if (errstr)
519 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
521 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
522 4626e416 2018-06-10 stsp *space = '\0';
523 bff6ca00 2018-04-23 stsp return NULL;
526 d1cda826 2017-11-06 stsp static const struct got_error *
527 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
529 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
530 d1cda826 2017-11-06 stsp char *s = buf;
531 d1cda826 2017-11-06 stsp size_t tlen;
532 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
534 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
535 d1cda826 2017-11-06 stsp if (*commit == NULL)
536 0a585a0d 2018-03-17 stsp return got_error_from_errno();
538 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
539 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
540 d1cda826 2017-11-06 stsp remain -= tlen;
541 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
542 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
546 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
547 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
550 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
551 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
553 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
557 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
558 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
559 d1cda826 2017-11-06 stsp remain -= tlen;
560 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
561 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
565 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
569 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
570 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
573 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
574 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
576 4626e416 2018-06-10 stsp size_t slen;
578 d1cda826 2017-11-06 stsp remain -= tlen;
579 d1cda826 2017-11-06 stsp if (remain <= 0) {
580 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
584 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
585 d1cda826 2017-11-06 stsp if (p == NULL) {
586 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
590 4626e416 2018-06-10 stsp slen = strlen(s);
591 6c281f94 2018-06-11 stsp err = parse_commit_time(&(*commit)->author_time,
592 6c281f94 2018-06-11 stsp &(*commit)->author_tzoff, s);
595 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
596 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
597 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
600 4626e416 2018-06-10 stsp s += slen + 1;
601 4626e416 2018-06-10 stsp remain -= slen + 1;
604 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
605 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
607 4626e416 2018-06-10 stsp size_t slen;
609 d1cda826 2017-11-06 stsp remain -= tlen;
610 d1cda826 2017-11-06 stsp if (remain <= 0) {
611 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
615 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
616 d1cda826 2017-11-06 stsp if (p == NULL) {
617 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
621 4626e416 2018-06-10 stsp slen = strlen(s);
622 6c281f94 2018-06-11 stsp err = parse_commit_time(&(*commit)->committer_time,
623 6c281f94 2018-06-11 stsp &(*commit)->committer_tzoff, s);
626 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
627 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
628 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
631 4626e416 2018-06-10 stsp s += slen + 1;
632 4626e416 2018-06-10 stsp remain -= slen + 1;
635 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
636 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
637 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
642 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
643 59ece79d 2018-02-12 stsp *commit = NULL;
645 0ffeb3c2 2017-11-26 stsp return err;
648 0ffeb3c2 2017-11-26 stsp static void
649 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
651 59ece79d 2018-02-12 stsp free(te->id);
652 0ffeb3c2 2017-11-26 stsp free(te->name);
656 e033d803 2018-04-23 stsp struct got_tree_entry *
657 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
659 e033d803 2018-04-23 stsp struct got_tree_entry *te;
661 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
662 e033d803 2018-04-23 stsp if (te == NULL)
663 e033d803 2018-04-23 stsp return NULL;
665 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
666 e033d803 2018-04-23 stsp if (te->id == NULL) {
673 0ffeb3c2 2017-11-26 stsp static const struct got_error *
674 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
675 0ffeb3c2 2017-11-26 stsp size_t maxlen)
677 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
678 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
680 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
681 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
682 0a585a0d 2018-03-17 stsp return got_error_from_errno();
684 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
685 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
687 59ece79d 2018-02-12 stsp *te = NULL;
688 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
691 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
692 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
693 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
695 59ece79d 2018-02-12 stsp *te = NULL;
696 0a585a0d 2018-03-17 stsp return err;
698 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
699 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
700 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
703 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
704 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
708 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
709 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
710 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
713 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
714 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
715 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
718 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
719 59ece79d 2018-02-12 stsp *te = NULL;
721 d1cda826 2017-11-06 stsp return err;
724 d1cda826 2017-11-06 stsp static const struct got_error *
725 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
727 90356acc 2018-01-27 stsp const struct got_error *err;
728 0ffeb3c2 2017-11-26 stsp size_t remain = len;
730 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
731 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
732 0a585a0d 2018-03-17 stsp return got_error_from_errno();
734 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
736 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
737 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
738 0ffeb3c2 2017-11-26 stsp size_t elen;
740 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
742 90356acc 2018-01-27 stsp return err;
743 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
744 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
745 0ffeb3c2 2017-11-26 stsp buf += elen;
746 0ffeb3c2 2017-11-26 stsp remain -= elen;
749 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
750 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
751 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
754 0ffeb3c2 2017-11-26 stsp return NULL;
757 0ffeb3c2 2017-11-26 stsp static const struct got_error *
758 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
760 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
761 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
762 eb651edf 2018-02-11 stsp size_t n, total, remain;
763 eb651edf 2018-02-11 stsp uint8_t *buf;
765 eb651edf 2018-02-11 stsp *outbuf = NULL;
766 eb651edf 2018-02-11 stsp *outlen = 0;
768 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
769 eb651edf 2018-02-11 stsp if (buf == NULL)
770 0a585a0d 2018-03-17 stsp return got_error_from_errno();
772 eb651edf 2018-02-11 stsp remain = blocksize;
774 eb651edf 2018-02-11 stsp while (1) {
775 eb651edf 2018-02-11 stsp if (remain == 0) {
776 eb651edf 2018-02-11 stsp uint8_t *newbuf;
777 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
778 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
779 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
782 eb651edf 2018-02-11 stsp buf = newbuf;
783 eb651edf 2018-02-11 stsp remain += blocksize;
785 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
786 eb651edf 2018-02-11 stsp if (n == 0) {
787 eb651edf 2018-02-11 stsp if (ferror(f)) {
788 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
791 eb651edf 2018-02-11 stsp break; /* EOF */
793 eb651edf 2018-02-11 stsp remain -= n;
794 eb651edf 2018-02-11 stsp total += n;
798 eb651edf 2018-02-11 stsp if (err == NULL) {
799 eb651edf 2018-02-11 stsp *outbuf = buf;
800 eb651edf 2018-02-11 stsp *outlen = total;
803 eb651edf 2018-02-11 stsp return err;
806 eb651edf 2018-02-11 stsp static const struct got_error *
807 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
810 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
811 d1cda826 2017-11-06 stsp size_t len;
812 eb651edf 2018-02-11 stsp uint8_t *p;
814 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
815 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
817 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
819 d1cda826 2017-11-06 stsp return err;
821 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
822 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
826 d1cda826 2017-11-06 stsp /* Skip object header. */
827 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
828 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
831 d1cda826 2017-11-06 stsp return err;
834 bff6ca00 2018-04-23 stsp static void
835 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
836 bff6ca00 2018-04-23 stsp int imsg_fds[2])
838 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
839 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
840 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
841 bff6ca00 2018-04-23 stsp FILE *f = NULL;
842 bff6ca00 2018-04-23 stsp int status = 0;
844 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
845 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
846 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
848 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
849 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
850 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
854 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
855 bff6ca00 2018-04-23 stsp if (f == NULL) {
856 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
857 bff6ca00 2018-04-23 stsp close(obj_fd);
861 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
865 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
867 bff6ca00 2018-04-23 stsp if (commit)
868 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
870 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
871 bff6ca00 2018-04-23 stsp status = 1;
875 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
876 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
877 bff6ca00 2018-04-23 stsp _exit(status);
880 bff6ca00 2018-04-23 stsp static const struct got_error *
881 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
882 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
884 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
885 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
886 bff6ca00 2018-04-23 stsp int imsg_fds[2];
889 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
890 bff6ca00 2018-04-23 stsp return got_error_from_errno();
892 bff6ca00 2018-04-23 stsp pid = fork();
893 bff6ca00 2018-04-23 stsp if (pid == -1)
894 bff6ca00 2018-04-23 stsp return got_error_from_errno();
895 bff6ca00 2018-04-23 stsp else if (pid == 0) {
896 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
897 e506bf32 2018-04-23 stsp /* not reached */
900 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
901 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
902 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
903 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
904 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
905 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
906 b419fc47 2018-04-26 stsp return err ? err : err_child;
909 d1cda826 2017-11-06 stsp const struct got_error *
910 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
911 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
913 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
915 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
916 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
918 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
919 ea35256b 2018-03-16 stsp uint8_t *buf;
920 ea35256b 2018-03-16 stsp size_t len;
921 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
923 ea35256b 2018-03-16 stsp return err;
924 b29656e2 2018-03-16 stsp obj->size = len;
925 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
929 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
931 ea35256b 2018-03-16 stsp return err;
932 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
935 d1cda826 2017-11-06 stsp return err;
939 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
941 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
943 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
944 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
945 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
946 79f35eb3 2018-06-11 stsp free(qid->id);
950 59ece79d 2018-02-12 stsp free(commit->tree_id);
951 d1cda826 2017-11-06 stsp free(commit->author);
952 7cd42a1a 2018-06-11 stsp free(commit->author_tzoff);
953 d1cda826 2017-11-06 stsp free(commit->committer);
954 7cd42a1a 2018-06-11 stsp free(commit->committer_tzoff);
955 d1cda826 2017-11-06 stsp free(commit->logmsg);
956 d1cda826 2017-11-06 stsp free(commit);
959 0ffeb3c2 2017-11-26 stsp static const struct got_error *
960 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
962 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
963 0ffeb3c2 2017-11-26 stsp size_t len;
964 eb651edf 2018-02-11 stsp uint8_t *p;
966 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
967 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
969 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
971 0ffeb3c2 2017-11-26 stsp return err;
973 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
974 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
978 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
979 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
980 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
983 e033d803 2018-04-23 stsp return err;
986 e033d803 2018-04-23 stsp static void
987 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
988 e033d803 2018-04-23 stsp int imsg_fds[2])
990 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
991 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
992 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
993 e033d803 2018-04-23 stsp FILE *f = NULL;
994 e033d803 2018-04-23 stsp int status = 0;
996 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
997 e033d803 2018-04-23 stsp close(imsg_fds[0]);
998 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
1000 e033d803 2018-04-23 stsp /* revoke access to most system calls */
1001 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
1002 e033d803 2018-04-23 stsp err = got_error_from_errno();
1003 e033d803 2018-04-23 stsp goto done;
1006 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
1007 e033d803 2018-04-23 stsp if (f == NULL) {
1008 e033d803 2018-04-23 stsp err = got_error_from_errno();
1009 e033d803 2018-04-23 stsp close(obj_fd);
1010 e033d803 2018-04-23 stsp goto done;
1013 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
1015 e033d803 2018-04-23 stsp goto done;
1017 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1020 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1021 e033d803 2018-04-23 stsp if (err) {
1022 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1023 e033d803 2018-04-23 stsp status = 1;
1026 e033d803 2018-04-23 stsp fclose(f);
1027 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1028 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1029 e033d803 2018-04-23 stsp _exit(status);
1032 e033d803 2018-04-23 stsp static const struct got_error *
1033 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1036 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1037 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1038 e033d803 2018-04-23 stsp int imsg_fds[2];
1039 e033d803 2018-04-23 stsp pid_t pid;
1041 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1042 e033d803 2018-04-23 stsp return got_error_from_errno();
1044 e033d803 2018-04-23 stsp pid = fork();
1045 e033d803 2018-04-23 stsp if (pid == -1)
1046 e033d803 2018-04-23 stsp return got_error_from_errno();
1047 e033d803 2018-04-23 stsp else if (pid == 0) {
1048 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1049 e033d803 2018-04-23 stsp /* not reached */
1052 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1053 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1054 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1055 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1056 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1057 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1058 b419fc47 2018-04-26 stsp return err ? err : err_child;
1061 0ffeb3c2 2017-11-26 stsp const struct got_error *
1062 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1063 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1065 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1067 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1068 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1070 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1071 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1072 e0ab43e7 2018-03-16 stsp size_t len;
1073 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1075 e0ab43e7 2018-03-16 stsp return err;
1076 b29656e2 2018-03-16 stsp obj->size = len;
1077 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1078 e0ab43e7 2018-03-16 stsp free(buf);
1081 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1083 e0ab43e7 2018-03-16 stsp return err;
1084 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1085 d5003b79 2018-04-22 stsp close(fd);
1087 0ffeb3c2 2017-11-26 stsp return err;
1091 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1093 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1095 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1096 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1097 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1098 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1101 f715ca7f 2017-11-27 stsp free(tree);
1104 ff6b18f8 2018-04-24 stsp static const struct got_error *
1105 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1107 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1108 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1109 ff6b18f8 2018-04-24 stsp int status = 0;
1110 2967a784 2018-04-24 stsp size_t size;
1111 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1113 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1114 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1115 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1117 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1118 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1119 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1120 ff6b18f8 2018-04-24 stsp goto done;
1123 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1124 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1125 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1126 8b2180d4 2018-04-26 stsp close(infd);
1127 8b2180d4 2018-04-26 stsp goto done;
1129 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1130 8b2180d4 2018-04-26 stsp fclose(infile);
1132 ff6b18f8 2018-04-24 stsp goto done;
1134 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1136 ff6b18f8 2018-04-24 stsp if (err) {
1137 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1138 ff6b18f8 2018-04-24 stsp status = 1;
1140 ff6b18f8 2018-04-24 stsp close(outfd);
1141 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1142 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1143 ff6b18f8 2018-04-24 stsp _exit(status);
1146 ff6b18f8 2018-04-24 stsp static const struct got_error *
1147 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1149 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1150 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1151 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1152 ff6b18f8 2018-04-24 stsp pid_t pid;
1154 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1155 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1157 ff6b18f8 2018-04-24 stsp pid = fork();
1158 ff6b18f8 2018-04-24 stsp if (pid == -1)
1159 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1160 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1161 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1162 ff6b18f8 2018-04-24 stsp /* not reached */
1165 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1166 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1167 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1168 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1169 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1170 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1171 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1172 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1173 b419fc47 2018-04-26 stsp return err ? err : err_child;
1176 68482ea3 2017-11-27 stsp const struct got_error *
1177 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1178 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1180 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1182 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1183 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1185 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1186 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1188 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1189 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1190 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1192 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1193 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1194 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1195 c7254d79 2018-04-24 stsp goto done;
1197 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1198 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1200 c7254d79 2018-04-24 stsp goto done;
1202 3aca5731 2018-04-24 stsp int infd, outfd;
1203 2967a784 2018-04-24 stsp size_t size;
1204 2967a784 2018-04-24 stsp struct stat sb;
1206 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1208 c7254d79 2018-04-24 stsp goto done;
1211 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1212 3aca5731 2018-04-24 stsp if (outfd == -1) {
1213 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1214 3aca5731 2018-04-24 stsp close(infd);
1215 3aca5731 2018-04-24 stsp goto done;
1218 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1219 3aca5731 2018-04-24 stsp close(infd);
1221 c7254d79 2018-04-24 stsp goto done;
1223 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1224 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1225 2967a784 2018-04-24 stsp close(outfd);
1226 2967a784 2018-04-24 stsp goto done;
1229 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1230 2967a784 2018-04-24 stsp err = got_error_from_errno();
1231 2967a784 2018-04-24 stsp close(outfd);
1232 2967a784 2018-04-24 stsp goto done;
1235 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1236 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1237 2967a784 2018-04-24 stsp close(outfd);
1238 2967a784 2018-04-24 stsp goto done;
1241 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1242 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1243 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1244 3aca5731 2018-04-24 stsp close(outfd);
1245 3aca5731 2018-04-24 stsp goto done;
1249 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1250 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1251 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1254 c7254d79 2018-04-24 stsp if (err && *blob) {
1255 c7254d79 2018-04-24 stsp if ((*blob)->f)
1256 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1257 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1258 c7254d79 2018-04-24 stsp free(*blob);
1259 c7254d79 2018-04-24 stsp *blob = NULL;
1261 68482ea3 2017-11-27 stsp return err;
1265 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1267 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1268 68482ea3 2017-11-27 stsp fclose(blob->f);
1269 68482ea3 2017-11-27 stsp free(blob);
1273 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1275 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1279 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1281 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1284 f934cf2c 2018-02-12 stsp const uint8_t *
1285 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1287 f934cf2c 2018-02-12 stsp return blob->read_buf;
1290 68482ea3 2017-11-27 stsp const struct got_error *
1291 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1295 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1296 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1297 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1298 eb651edf 2018-02-11 stsp *outlenp = n;
1299 eb651edf 2018-02-11 stsp return NULL;