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 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
445 bff6ca00 2018-04-23 stsp const char *id_str)
447 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
448 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
450 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
451 79f35eb3 2018-06-11 stsp if (qid == NULL)
452 bff6ca00 2018-04-23 stsp return got_error_from_errno();
454 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
455 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
456 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
458 bff6ca00 2018-04-23 stsp return err;
461 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
462 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
463 79f35eb3 2018-06-11 stsp free(qid->id);
465 bff6ca00 2018-04-23 stsp return err;
468 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
469 bff6ca00 2018-04-23 stsp commit->nparents++;
471 4626e416 2018-06-10 stsp return NULL;
474 4626e416 2018-06-10 stsp static const struct got_error *
475 6c281f94 2018-06-11 stsp parse_commit_time(time_t *time, char **tzoff, char *committer)
477 4626e416 2018-06-10 stsp const char *errstr;
478 4626e416 2018-06-10 stsp char *space;
482 6c281f94 2018-06-11 stsp /* Parse and then strip trailing timezone indicator. */
483 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
484 4626e416 2018-06-10 stsp if (space == NULL)
485 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
486 6c281f94 2018-06-11 stsp *tzoff = strdup(space + 1);
487 6c281f94 2018-06-11 stsp if (*tzoff == NULL)
488 6c281f94 2018-06-11 stsp return got_error_from_errno();
489 4626e416 2018-06-10 stsp *space = '\0';
491 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
492 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
493 4626e416 2018-06-10 stsp if (space == NULL)
494 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
496 4626e416 2018-06-10 stsp *time = strtonum(space + 1, 0, INT64_MAX, &errstr);
497 4626e416 2018-06-10 stsp if (errstr)
498 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
500 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
501 4626e416 2018-06-10 stsp *space = '\0';
502 bff6ca00 2018-04-23 stsp return NULL;
505 d1cda826 2017-11-06 stsp static const struct got_error *
506 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
508 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
509 d1cda826 2017-11-06 stsp char *s = buf;
510 d1cda826 2017-11-06 stsp size_t tlen;
511 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
513 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
514 d1cda826 2017-11-06 stsp if (*commit == NULL)
515 0a585a0d 2018-03-17 stsp return got_error_from_errno();
517 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
518 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
519 d1cda826 2017-11-06 stsp remain -= tlen;
520 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
521 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
525 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
526 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
529 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
530 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
532 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
536 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
537 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
538 d1cda826 2017-11-06 stsp remain -= tlen;
539 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
540 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
544 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
548 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
549 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
552 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
553 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
555 4626e416 2018-06-10 stsp size_t slen;
557 d1cda826 2017-11-06 stsp remain -= tlen;
558 d1cda826 2017-11-06 stsp if (remain <= 0) {
559 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
563 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
564 d1cda826 2017-11-06 stsp if (p == NULL) {
565 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
569 4626e416 2018-06-10 stsp slen = strlen(s);
570 6c281f94 2018-06-11 stsp err = parse_commit_time(&(*commit)->author_time,
571 6c281f94 2018-06-11 stsp &(*commit)->author_tzoff, s);
574 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
575 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
576 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
579 4626e416 2018-06-10 stsp s += slen + 1;
580 4626e416 2018-06-10 stsp remain -= slen + 1;
583 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
584 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
586 4626e416 2018-06-10 stsp size_t slen;
588 d1cda826 2017-11-06 stsp remain -= tlen;
589 d1cda826 2017-11-06 stsp if (remain <= 0) {
590 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
594 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
595 d1cda826 2017-11-06 stsp if (p == NULL) {
596 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
600 4626e416 2018-06-10 stsp slen = strlen(s);
601 6c281f94 2018-06-11 stsp err = parse_commit_time(&(*commit)->committer_time,
602 6c281f94 2018-06-11 stsp &(*commit)->committer_tzoff, s);
605 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
606 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
607 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
610 4626e416 2018-06-10 stsp s += slen + 1;
611 4626e416 2018-06-10 stsp remain -= slen + 1;
614 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
615 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
616 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
621 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
622 59ece79d 2018-02-12 stsp *commit = NULL;
624 0ffeb3c2 2017-11-26 stsp return err;
627 0ffeb3c2 2017-11-26 stsp static void
628 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
630 59ece79d 2018-02-12 stsp free(te->id);
631 0ffeb3c2 2017-11-26 stsp free(te->name);
635 e033d803 2018-04-23 stsp struct got_tree_entry *
636 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
638 e033d803 2018-04-23 stsp struct got_tree_entry *te;
640 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
641 e033d803 2018-04-23 stsp if (te == NULL)
642 e033d803 2018-04-23 stsp return NULL;
644 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
645 e033d803 2018-04-23 stsp if (te->id == NULL) {
652 0ffeb3c2 2017-11-26 stsp static const struct got_error *
653 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
654 0ffeb3c2 2017-11-26 stsp size_t maxlen)
656 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
657 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
659 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
660 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
661 0a585a0d 2018-03-17 stsp return got_error_from_errno();
663 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
664 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
666 59ece79d 2018-02-12 stsp *te = NULL;
667 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
670 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
671 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
672 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
674 59ece79d 2018-02-12 stsp *te = NULL;
675 0a585a0d 2018-03-17 stsp return err;
677 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
678 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
679 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
682 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
683 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
687 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
688 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
689 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
692 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
693 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
694 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
697 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
698 59ece79d 2018-02-12 stsp *te = NULL;
700 d1cda826 2017-11-06 stsp return err;
703 d1cda826 2017-11-06 stsp static const struct got_error *
704 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
706 90356acc 2018-01-27 stsp const struct got_error *err;
707 0ffeb3c2 2017-11-26 stsp size_t remain = len;
709 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
710 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
711 0a585a0d 2018-03-17 stsp return got_error_from_errno();
713 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
715 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
716 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
717 0ffeb3c2 2017-11-26 stsp size_t elen;
719 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
721 90356acc 2018-01-27 stsp return err;
722 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
723 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
724 0ffeb3c2 2017-11-26 stsp buf += elen;
725 0ffeb3c2 2017-11-26 stsp remain -= elen;
728 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
729 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
730 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
733 0ffeb3c2 2017-11-26 stsp return NULL;
736 0ffeb3c2 2017-11-26 stsp static const struct got_error *
737 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
739 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
740 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
741 eb651edf 2018-02-11 stsp size_t n, total, remain;
742 eb651edf 2018-02-11 stsp uint8_t *buf;
744 eb651edf 2018-02-11 stsp *outbuf = NULL;
745 eb651edf 2018-02-11 stsp *outlen = 0;
747 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
748 eb651edf 2018-02-11 stsp if (buf == NULL)
749 0a585a0d 2018-03-17 stsp return got_error_from_errno();
751 eb651edf 2018-02-11 stsp remain = blocksize;
753 eb651edf 2018-02-11 stsp while (1) {
754 eb651edf 2018-02-11 stsp if (remain == 0) {
755 eb651edf 2018-02-11 stsp uint8_t *newbuf;
756 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
757 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
758 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
761 eb651edf 2018-02-11 stsp buf = newbuf;
762 eb651edf 2018-02-11 stsp remain += blocksize;
764 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
765 eb651edf 2018-02-11 stsp if (n == 0) {
766 eb651edf 2018-02-11 stsp if (ferror(f)) {
767 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
770 eb651edf 2018-02-11 stsp break; /* EOF */
772 eb651edf 2018-02-11 stsp remain -= n;
773 eb651edf 2018-02-11 stsp total += n;
777 eb651edf 2018-02-11 stsp if (err == NULL) {
778 eb651edf 2018-02-11 stsp *outbuf = buf;
779 eb651edf 2018-02-11 stsp *outlen = total;
782 eb651edf 2018-02-11 stsp return err;
785 eb651edf 2018-02-11 stsp static const struct got_error *
786 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
789 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
790 d1cda826 2017-11-06 stsp size_t len;
791 eb651edf 2018-02-11 stsp uint8_t *p;
793 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
794 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
796 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
798 d1cda826 2017-11-06 stsp return err;
800 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
801 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
805 d1cda826 2017-11-06 stsp /* Skip object header. */
806 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
807 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
810 d1cda826 2017-11-06 stsp return err;
813 bff6ca00 2018-04-23 stsp static void
814 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
815 bff6ca00 2018-04-23 stsp int imsg_fds[2])
817 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
818 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
819 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
820 bff6ca00 2018-04-23 stsp FILE *f = NULL;
821 bff6ca00 2018-04-23 stsp int status = 0;
823 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
824 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
825 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
827 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
828 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
829 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
833 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
834 bff6ca00 2018-04-23 stsp if (f == NULL) {
835 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
836 bff6ca00 2018-04-23 stsp close(obj_fd);
840 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
844 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
846 bff6ca00 2018-04-23 stsp if (commit)
847 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
849 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
850 bff6ca00 2018-04-23 stsp status = 1;
854 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
855 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
856 bff6ca00 2018-04-23 stsp _exit(status);
859 bff6ca00 2018-04-23 stsp static const struct got_error *
860 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
861 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
863 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
864 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
865 bff6ca00 2018-04-23 stsp int imsg_fds[2];
868 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
869 bff6ca00 2018-04-23 stsp return got_error_from_errno();
871 bff6ca00 2018-04-23 stsp pid = fork();
872 bff6ca00 2018-04-23 stsp if (pid == -1)
873 bff6ca00 2018-04-23 stsp return got_error_from_errno();
874 bff6ca00 2018-04-23 stsp else if (pid == 0) {
875 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
876 e506bf32 2018-04-23 stsp /* not reached */
879 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
880 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
881 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
882 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
883 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
884 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
885 b419fc47 2018-04-26 stsp return err ? err : err_child;
888 d1cda826 2017-11-06 stsp const struct got_error *
889 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
890 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
892 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
894 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
895 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
897 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
898 ea35256b 2018-03-16 stsp uint8_t *buf;
899 ea35256b 2018-03-16 stsp size_t len;
900 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
902 ea35256b 2018-03-16 stsp return err;
903 b29656e2 2018-03-16 stsp obj->size = len;
904 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
908 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
910 ea35256b 2018-03-16 stsp return err;
911 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
914 d1cda826 2017-11-06 stsp return err;
918 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
920 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
922 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
923 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
924 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
925 79f35eb3 2018-06-11 stsp free(qid->id);
929 59ece79d 2018-02-12 stsp free(commit->tree_id);
930 d1cda826 2017-11-06 stsp free(commit->author);
931 7cd42a1a 2018-06-11 stsp free(commit->author_tzoff);
932 d1cda826 2017-11-06 stsp free(commit->committer);
933 7cd42a1a 2018-06-11 stsp free(commit->committer_tzoff);
934 d1cda826 2017-11-06 stsp free(commit->logmsg);
935 d1cda826 2017-11-06 stsp free(commit);
938 0ffeb3c2 2017-11-26 stsp static const struct got_error *
939 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
941 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
942 0ffeb3c2 2017-11-26 stsp size_t len;
943 eb651edf 2018-02-11 stsp uint8_t *p;
945 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
946 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
948 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
950 0ffeb3c2 2017-11-26 stsp return err;
952 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
953 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
957 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
958 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
959 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
962 e033d803 2018-04-23 stsp return err;
965 e033d803 2018-04-23 stsp static void
966 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
967 e033d803 2018-04-23 stsp int imsg_fds[2])
969 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
970 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
971 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
972 e033d803 2018-04-23 stsp FILE *f = NULL;
973 e033d803 2018-04-23 stsp int status = 0;
975 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
976 e033d803 2018-04-23 stsp close(imsg_fds[0]);
977 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
979 e033d803 2018-04-23 stsp /* revoke access to most system calls */
980 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
981 e033d803 2018-04-23 stsp err = got_error_from_errno();
985 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
986 e033d803 2018-04-23 stsp if (f == NULL) {
987 e033d803 2018-04-23 stsp err = got_error_from_errno();
988 e033d803 2018-04-23 stsp close(obj_fd);
992 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
996 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
999 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1000 e033d803 2018-04-23 stsp if (err) {
1001 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1002 e033d803 2018-04-23 stsp status = 1;
1005 e033d803 2018-04-23 stsp fclose(f);
1006 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1007 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1008 e033d803 2018-04-23 stsp _exit(status);
1011 e033d803 2018-04-23 stsp static const struct got_error *
1012 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1015 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1016 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1017 e033d803 2018-04-23 stsp int imsg_fds[2];
1018 e033d803 2018-04-23 stsp pid_t pid;
1020 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1021 e033d803 2018-04-23 stsp return got_error_from_errno();
1023 e033d803 2018-04-23 stsp pid = fork();
1024 e033d803 2018-04-23 stsp if (pid == -1)
1025 e033d803 2018-04-23 stsp return got_error_from_errno();
1026 e033d803 2018-04-23 stsp else if (pid == 0) {
1027 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1028 e033d803 2018-04-23 stsp /* not reached */
1031 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1032 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1033 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1034 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1035 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1036 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1037 b419fc47 2018-04-26 stsp return err ? err : err_child;
1040 0ffeb3c2 2017-11-26 stsp const struct got_error *
1041 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1042 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1044 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1046 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1047 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1049 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1050 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1051 e0ab43e7 2018-03-16 stsp size_t len;
1052 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1054 e0ab43e7 2018-03-16 stsp return err;
1055 b29656e2 2018-03-16 stsp obj->size = len;
1056 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1057 e0ab43e7 2018-03-16 stsp free(buf);
1060 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1062 e0ab43e7 2018-03-16 stsp return err;
1063 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1064 d5003b79 2018-04-22 stsp close(fd);
1066 0ffeb3c2 2017-11-26 stsp return err;
1070 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1072 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1074 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1075 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1076 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1077 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1080 f715ca7f 2017-11-27 stsp free(tree);
1083 ff6b18f8 2018-04-24 stsp static const struct got_error *
1084 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1086 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1087 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1088 ff6b18f8 2018-04-24 stsp int status = 0;
1089 2967a784 2018-04-24 stsp size_t size;
1090 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1092 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1093 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1094 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1096 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1097 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1098 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1099 ff6b18f8 2018-04-24 stsp goto done;
1102 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1103 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1104 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1105 8b2180d4 2018-04-26 stsp close(infd);
1106 8b2180d4 2018-04-26 stsp goto done;
1108 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1109 8b2180d4 2018-04-26 stsp fclose(infile);
1111 ff6b18f8 2018-04-24 stsp goto done;
1113 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1115 ff6b18f8 2018-04-24 stsp if (err) {
1116 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1117 ff6b18f8 2018-04-24 stsp status = 1;
1119 ff6b18f8 2018-04-24 stsp close(outfd);
1120 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1121 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1122 ff6b18f8 2018-04-24 stsp _exit(status);
1125 ff6b18f8 2018-04-24 stsp static const struct got_error *
1126 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1128 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1129 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1130 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1131 ff6b18f8 2018-04-24 stsp pid_t pid;
1133 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1134 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1136 ff6b18f8 2018-04-24 stsp pid = fork();
1137 ff6b18f8 2018-04-24 stsp if (pid == -1)
1138 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1139 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1140 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1141 ff6b18f8 2018-04-24 stsp /* not reached */
1144 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1145 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1146 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1147 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1148 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1149 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1150 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1151 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1152 b419fc47 2018-04-26 stsp return err ? err : err_child;
1155 68482ea3 2017-11-27 stsp const struct got_error *
1156 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1157 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1159 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1161 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1162 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1164 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1165 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1167 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1168 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1169 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1171 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1172 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1173 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1174 c7254d79 2018-04-24 stsp goto done;
1176 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1177 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1179 c7254d79 2018-04-24 stsp goto done;
1181 3aca5731 2018-04-24 stsp int infd, outfd;
1182 2967a784 2018-04-24 stsp size_t size;
1183 2967a784 2018-04-24 stsp struct stat sb;
1185 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1187 c7254d79 2018-04-24 stsp goto done;
1190 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1191 3aca5731 2018-04-24 stsp if (outfd == -1) {
1192 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1193 3aca5731 2018-04-24 stsp close(infd);
1194 3aca5731 2018-04-24 stsp goto done;
1197 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1198 3aca5731 2018-04-24 stsp close(infd);
1200 c7254d79 2018-04-24 stsp goto done;
1202 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1203 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1204 2967a784 2018-04-24 stsp close(outfd);
1205 2967a784 2018-04-24 stsp goto done;
1208 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1209 2967a784 2018-04-24 stsp err = got_error_from_errno();
1210 2967a784 2018-04-24 stsp close(outfd);
1211 2967a784 2018-04-24 stsp goto done;
1214 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1215 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1216 2967a784 2018-04-24 stsp close(outfd);
1217 2967a784 2018-04-24 stsp goto done;
1220 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1221 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1222 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1223 3aca5731 2018-04-24 stsp close(outfd);
1224 3aca5731 2018-04-24 stsp goto done;
1228 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1229 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1230 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1233 c7254d79 2018-04-24 stsp if (err && *blob) {
1234 c7254d79 2018-04-24 stsp if ((*blob)->f)
1235 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1236 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1237 c7254d79 2018-04-24 stsp free(*blob);
1238 c7254d79 2018-04-24 stsp *blob = NULL;
1240 68482ea3 2017-11-27 stsp return err;
1244 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1246 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1247 68482ea3 2017-11-27 stsp fclose(blob->f);
1248 68482ea3 2017-11-27 stsp free(blob);
1252 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1254 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1258 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1260 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1263 f934cf2c 2018-02-12 stsp const uint8_t *
1264 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1266 f934cf2c 2018-02-12 stsp return blob->read_buf;
1269 68482ea3 2017-11-27 stsp const struct got_error *
1270 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1274 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1275 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1276 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1277 eb651edf 2018-02-11 stsp *outlenp = n;
1278 eb651edf 2018-02-11 stsp return NULL;