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>
35 788c352e 2018-06-16 stsp #include <time.h>
37 ab9a70b2 2017-11-06 stsp #include "got_error.h"
38 d71d75ad 2017-11-05 stsp #include "got_object.h"
39 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
40 511a516b 2018-05-19 stsp #include "got_opentemp.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
45 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
48 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 ab9a70b2 2017-11-06 stsp #ifndef nitems
55 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
58 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
59 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
60 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
62 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
64 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
65 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
67 ef0981d5 2018-02-12 stsp const struct got_error *
68 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
70 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
72 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
73 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
74 0a585a0d 2018-03-17 stsp return got_error_from_errno();
76 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
77 ef0981d5 2018-02-12 stsp free(*outbuf);
78 ef0981d5 2018-02-12 stsp *outbuf = NULL;
79 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
82 ef0981d5 2018-02-12 stsp return NULL;
86 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
88 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
91 8bf5b3c9 2018-03-17 stsp struct got_object_id *
92 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
94 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
96 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
97 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
98 8bf5b3c9 2018-03-17 stsp return NULL;
99 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
100 8bf5b3c9 2018-03-17 stsp return id2;
103 3235492e 2018-04-01 stsp struct got_object_id *
104 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
106 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
109 bacc9935 2018-05-20 stsp const struct got_error *
110 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
112 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
116 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
118 b107e67f 2018-01-19 stsp switch (obj->type) {
119 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
120 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
121 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
122 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
123 b107e67f 2018-01-19 stsp return obj->type;
129 96f5e8b3 2018-01-23 stsp /* not reached */
133 ab9a70b2 2017-11-06 stsp static const struct got_error *
134 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
136 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
137 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
138 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
139 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
141 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
142 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
143 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
144 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
146 ab9a70b2 2017-11-06 stsp int type = 0;
147 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
149 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
151 ab9a70b2 2017-11-06 stsp if (p == NULL)
152 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
154 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
156 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
157 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
158 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
159 ab9a70b2 2017-11-06 stsp const char *errstr;
161 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
164 ab9a70b2 2017-11-06 stsp type = obj_types[i];
165 63323519 2017-11-06 stsp if (len <= tlen)
166 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
167 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
168 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
169 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
173 ab9a70b2 2017-11-06 stsp if (type == 0)
174 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
176 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
177 1db76ab5 2018-01-26 mpi if (*obj == NULL)
178 0a585a0d 2018-03-17 stsp return got_error_from_errno();
179 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
180 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
181 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
182 ab9a70b2 2017-11-06 stsp return NULL;
185 ab9a70b2 2017-11-06 stsp static const struct got_error *
186 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
188 ab9a70b2 2017-11-06 stsp const struct got_error *err;
189 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
191 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
192 744d9326 2017-12-01 stsp size_t outlen, totlen;
195 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
196 a3e2cbea 2017-12-01 stsp if (buf == NULL)
197 0a585a0d 2018-03-17 stsp return got_error_from_errno();
199 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
201 ab9a70b2 2017-11-06 stsp return err;
204 744d9326 2017-12-01 stsp totlen = 0;
206 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
209 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
210 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
211 e302c59e 2017-12-01 stsp if (buf == NULL) {
212 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
216 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
217 744d9326 2017-12-01 stsp totlen += outlen;
219 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
221 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
223 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
224 2178c42e 2018-04-22 stsp return err;
227 302b7dd6 2018-04-23 stsp static void
228 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
230 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
231 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
232 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
233 302b7dd6 2018-04-23 stsp FILE *f = NULL;
234 302b7dd6 2018-04-23 stsp int status = 0;
236 be37c2e6 2018-04-24 stsp setproctitle("read object header");
237 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
238 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
240 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
241 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
242 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
246 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
247 302b7dd6 2018-04-23 stsp if (f == NULL) {
248 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
249 302b7dd6 2018-04-23 stsp close(obj_fd);
253 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
257 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
260 e3306bd9 2018-04-23 stsp got_object_close(obj);
262 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
263 302b7dd6 2018-04-23 stsp status = 1;
267 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
268 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
269 302b7dd6 2018-04-23 stsp _exit(status);
272 b419fc47 2018-04-26 stsp static const struct got_error *
273 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
275 b419fc47 2018-04-26 stsp int child_status;
277 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
279 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
280 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
282 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
283 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
285 b419fc47 2018-04-26 stsp return NULL;
288 2178c42e 2018-04-22 stsp static const struct got_error *
289 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
291 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
292 2178c42e 2018-04-22 stsp int imsg_fds[2];
293 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
296 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
297 2178c42e 2018-04-22 stsp return got_error_from_errno();
299 2178c42e 2018-04-22 stsp pid = fork();
300 2178c42e 2018-04-22 stsp if (pid == -1)
301 2178c42e 2018-04-22 stsp return got_error_from_errno();
302 2178c42e 2018-04-22 stsp else if (pid == 0) {
303 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
304 302b7dd6 2018-04-23 stsp /* not reached */
307 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
308 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
309 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
310 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
311 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
312 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
313 b419fc47 2018-04-26 stsp return err ? err : err_child;
316 d1cda826 2017-11-06 stsp static const struct got_error *
317 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
319 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
321 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
323 e6b1056e 2018-04-22 stsp *path = NULL;
325 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
326 0a585a0d 2018-03-17 stsp return got_error_from_errno();
328 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
330 ef0981d5 2018-02-12 stsp return err;
332 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
333 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
334 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
337 d1cda826 2017-11-06 stsp free(path_objects);
338 d1cda826 2017-11-06 stsp return err;
341 4ee4114f 2018-01-23 stsp static const struct got_error *
342 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
344 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
345 a1fd68d8 2018-01-12 stsp char *path;
347 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
349 d1cda826 2017-11-06 stsp return err;
350 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
351 d5003b79 2018-04-22 stsp if (*fd == -1) {
352 6c00b545 2018-01-17 stsp err = got_error_from_errno();
356 4558fcd4 2018-01-14 stsp free(path);
357 4558fcd4 2018-01-14 stsp return err;
360 4558fcd4 2018-01-14 stsp const struct got_error *
361 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
362 4558fcd4 2018-01-14 stsp struct got_object_id *id)
364 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
365 6c00b545 2018-01-17 stsp char *path;
368 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
370 4558fcd4 2018-01-14 stsp return err;
372 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
373 2178c42e 2018-04-22 stsp if (fd == -1) {
374 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
375 6c00b545 2018-01-17 stsp err = got_error_from_errno();
378 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
381 6c00b545 2018-01-17 stsp if (*obj == NULL)
382 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
384 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
387 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
390 6c00b545 2018-01-17 stsp free(path);
391 2178c42e 2018-04-22 stsp if (fd != -1)
393 ab9a70b2 2017-11-06 stsp return err;
397 6dfa2fd3 2018-02-12 stsp const struct got_error *
398 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
399 6dfa2fd3 2018-02-12 stsp const char *id_str)
401 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
403 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
404 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
406 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
410 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
412 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
413 c3703302 2018-01-23 stsp struct got_delta *delta;
414 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
415 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
416 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
417 c3703302 2018-01-23 stsp got_delta_close(delta);
420 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
421 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
425 bff6ca00 2018-04-23 stsp struct got_commit_object *
426 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
428 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
430 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
431 bff6ca00 2018-04-23 stsp if (commit == NULL)
432 bff6ca00 2018-04-23 stsp return NULL;
433 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
434 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
435 bff6ca00 2018-04-23 stsp free(commit);
436 bff6ca00 2018-04-23 stsp return NULL;
439 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
441 bff6ca00 2018-04-23 stsp return commit;
444 bff6ca00 2018-04-23 stsp const struct got_error *
445 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
446 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
448 be6a1b5a 2018-06-11 stsp const struct got_error *err;
449 be6a1b5a 2018-06-11 stsp struct got_object *obj;
451 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
453 be6a1b5a 2018-06-11 stsp return err;
454 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
455 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
459 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
461 be6a1b5a 2018-06-11 stsp got_object_close(obj);
462 be6a1b5a 2018-06-11 stsp return err;
465 be6a1b5a 2018-06-11 stsp const struct got_error *
466 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
467 bff6ca00 2018-04-23 stsp const char *id_str)
469 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
470 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
472 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
473 79f35eb3 2018-06-11 stsp if (qid == NULL)
474 bff6ca00 2018-04-23 stsp return got_error_from_errno();
476 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
477 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
478 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
480 bff6ca00 2018-04-23 stsp return err;
483 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
484 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
485 79f35eb3 2018-06-11 stsp free(qid->id);
487 bff6ca00 2018-04-23 stsp return err;
490 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
491 bff6ca00 2018-04-23 stsp commit->nparents++;
493 4626e416 2018-06-10 stsp return NULL;
496 4626e416 2018-06-10 stsp static const struct got_error *
497 788c352e 2018-06-16 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
499 788c352e 2018-06-16 stsp int sign = 1;
500 788c352e 2018-06-16 stsp const char *p = tzstr;
501 788c352e 2018-06-16 stsp time_t h, m;
503 788c352e 2018-06-16 stsp *gmtoff = 0;
505 788c352e 2018-06-16 stsp if (*p == '-')
507 788c352e 2018-06-16 stsp else if (*p != '+')
508 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
510 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
511 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
512 788c352e 2018-06-16 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
515 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
516 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
517 788c352e 2018-06-16 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
519 788c352e 2018-06-16 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
520 788c352e 2018-06-16 stsp return NULL;
523 788c352e 2018-06-16 stsp static const struct got_error *
524 788c352e 2018-06-16 stsp parse_commit_time(struct tm *tm, char *committer)
526 788c352e 2018-06-16 stsp const struct got_error *err = NULL;
527 788c352e 2018-06-16 stsp const char *errstr;
528 788c352e 2018-06-16 stsp char *space, *tzstr;
529 788c352e 2018-06-16 stsp time_t gmtoff;
530 788c352e 2018-06-16 stsp time_t time;
532 788c352e 2018-06-16 stsp /* Parse and strip off trailing timezone indicator string. */
533 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
534 4626e416 2018-06-10 stsp if (space == NULL)
535 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
536 788c352e 2018-06-16 stsp tzstr = strdup(space + 1);
537 788c352e 2018-06-16 stsp if (tzstr == NULL)
538 6c281f94 2018-06-11 stsp return got_error_from_errno();
539 788c352e 2018-06-16 stsp err = parse_gmtoff(&gmtoff, tzstr);
540 788c352e 2018-06-16 stsp free(tzstr);
542 788c352e 2018-06-16 stsp return err;
543 4626e416 2018-06-10 stsp *space = '\0';
545 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
546 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
547 4626e416 2018-06-10 stsp if (space == NULL)
548 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
550 788c352e 2018-06-16 stsp /* Timestamp parsed here is expressed in comitter's local time. */
551 788c352e 2018-06-16 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
552 4626e416 2018-06-10 stsp if (errstr)
553 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
555 788c352e 2018-06-16 stsp /* Express the time stamp in UTC. */
556 788c352e 2018-06-16 stsp memset(tm, 0, sizeof(*tm));
557 788c352e 2018-06-16 stsp time -= gmtoff;
558 788c352e 2018-06-16 stsp if (localtime_r(&time, tm) == NULL)
559 788c352e 2018-06-16 stsp return got_error_from_errno();
560 788c352e 2018-06-16 stsp tm->tm_gmtoff = gmtoff;
562 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
563 4626e416 2018-06-10 stsp *space = '\0';
565 bff6ca00 2018-04-23 stsp return NULL;
568 d1cda826 2017-11-06 stsp static const struct got_error *
569 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
571 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
572 d1cda826 2017-11-06 stsp char *s = buf;
573 d1cda826 2017-11-06 stsp size_t tlen;
574 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
576 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
577 d1cda826 2017-11-06 stsp if (*commit == NULL)
578 0a585a0d 2018-03-17 stsp return got_error_from_errno();
580 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
581 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, tlen) == 0) {
582 d1cda826 2017-11-06 stsp remain -= tlen;
583 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
584 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
588 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
589 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
592 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
593 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
595 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
599 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
600 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
601 d1cda826 2017-11-06 stsp remain -= tlen;
602 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
603 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
607 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
611 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
612 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
615 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
616 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
618 4626e416 2018-06-10 stsp size_t slen;
620 d1cda826 2017-11-06 stsp remain -= tlen;
621 d1cda826 2017-11-06 stsp if (remain <= 0) {
622 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
627 d1cda826 2017-11-06 stsp if (p == NULL) {
628 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
632 4626e416 2018-06-10 stsp slen = strlen(s);
633 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_author, s);
636 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
637 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
638 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
641 4626e416 2018-06-10 stsp s += slen + 1;
642 4626e416 2018-06-10 stsp remain -= slen + 1;
645 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
646 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
648 4626e416 2018-06-10 stsp size_t slen;
650 d1cda826 2017-11-06 stsp remain -= tlen;
651 d1cda826 2017-11-06 stsp if (remain <= 0) {
652 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
656 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
657 d1cda826 2017-11-06 stsp if (p == NULL) {
658 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
662 4626e416 2018-06-10 stsp slen = strlen(s);
663 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
666 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
667 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
668 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
671 4626e416 2018-06-10 stsp s += slen + 1;
672 4626e416 2018-06-10 stsp remain -= slen + 1;
675 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
676 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
677 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
682 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
683 59ece79d 2018-02-12 stsp *commit = NULL;
685 0ffeb3c2 2017-11-26 stsp return err;
688 0ffeb3c2 2017-11-26 stsp static void
689 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
691 59ece79d 2018-02-12 stsp free(te->id);
692 0ffeb3c2 2017-11-26 stsp free(te->name);
696 e033d803 2018-04-23 stsp struct got_tree_entry *
697 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
699 e033d803 2018-04-23 stsp struct got_tree_entry *te;
701 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
702 e033d803 2018-04-23 stsp if (te == NULL)
703 e033d803 2018-04-23 stsp return NULL;
705 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
706 e033d803 2018-04-23 stsp if (te->id == NULL) {
713 0ffeb3c2 2017-11-26 stsp static const struct got_error *
714 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
715 0ffeb3c2 2017-11-26 stsp size_t maxlen)
717 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
718 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
720 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
721 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
722 0a585a0d 2018-03-17 stsp return got_error_from_errno();
724 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
725 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
727 59ece79d 2018-02-12 stsp *te = NULL;
728 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
731 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
732 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
733 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
735 59ece79d 2018-02-12 stsp *te = NULL;
736 0a585a0d 2018-03-17 stsp return err;
738 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
739 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
740 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
743 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
744 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
748 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
749 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
750 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
753 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
754 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
755 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
758 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
759 59ece79d 2018-02-12 stsp *te = NULL;
761 d1cda826 2017-11-06 stsp return err;
764 d1cda826 2017-11-06 stsp static const struct got_error *
765 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
767 90356acc 2018-01-27 stsp const struct got_error *err;
768 0ffeb3c2 2017-11-26 stsp size_t remain = len;
770 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
771 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
772 0a585a0d 2018-03-17 stsp return got_error_from_errno();
774 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
776 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
777 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
778 0ffeb3c2 2017-11-26 stsp size_t elen;
780 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
782 90356acc 2018-01-27 stsp return err;
783 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
784 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
785 0ffeb3c2 2017-11-26 stsp buf += elen;
786 0ffeb3c2 2017-11-26 stsp remain -= elen;
789 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
790 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
791 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
794 0ffeb3c2 2017-11-26 stsp return NULL;
797 0ffeb3c2 2017-11-26 stsp static const struct got_error *
798 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
800 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
801 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
802 eb651edf 2018-02-11 stsp size_t n, total, remain;
803 eb651edf 2018-02-11 stsp uint8_t *buf;
805 eb651edf 2018-02-11 stsp *outbuf = NULL;
806 eb651edf 2018-02-11 stsp *outlen = 0;
808 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
809 eb651edf 2018-02-11 stsp if (buf == NULL)
810 0a585a0d 2018-03-17 stsp return got_error_from_errno();
812 eb651edf 2018-02-11 stsp remain = blocksize;
814 eb651edf 2018-02-11 stsp while (1) {
815 eb651edf 2018-02-11 stsp if (remain == 0) {
816 eb651edf 2018-02-11 stsp uint8_t *newbuf;
817 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
818 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
819 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
822 eb651edf 2018-02-11 stsp buf = newbuf;
823 eb651edf 2018-02-11 stsp remain += blocksize;
825 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
826 eb651edf 2018-02-11 stsp if (n == 0) {
827 eb651edf 2018-02-11 stsp if (ferror(f)) {
828 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
831 eb651edf 2018-02-11 stsp break; /* EOF */
833 eb651edf 2018-02-11 stsp remain -= n;
834 eb651edf 2018-02-11 stsp total += n;
838 eb651edf 2018-02-11 stsp if (err == NULL) {
839 eb651edf 2018-02-11 stsp *outbuf = buf;
840 eb651edf 2018-02-11 stsp *outlen = total;
843 eb651edf 2018-02-11 stsp return err;
846 eb651edf 2018-02-11 stsp static const struct got_error *
847 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
850 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
851 d1cda826 2017-11-06 stsp size_t len;
852 eb651edf 2018-02-11 stsp uint8_t *p;
854 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
855 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
857 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
859 d1cda826 2017-11-06 stsp return err;
861 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
862 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
866 d1cda826 2017-11-06 stsp /* Skip object header. */
867 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
868 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
871 d1cda826 2017-11-06 stsp return err;
874 bff6ca00 2018-04-23 stsp static void
875 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
876 bff6ca00 2018-04-23 stsp int imsg_fds[2])
878 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
879 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
880 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
881 bff6ca00 2018-04-23 stsp FILE *f = NULL;
882 bff6ca00 2018-04-23 stsp int status = 0;
884 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
885 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
886 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
888 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
889 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
890 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
894 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
895 bff6ca00 2018-04-23 stsp if (f == NULL) {
896 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
897 bff6ca00 2018-04-23 stsp close(obj_fd);
901 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
905 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
907 bff6ca00 2018-04-23 stsp if (commit)
908 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
910 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
911 bff6ca00 2018-04-23 stsp status = 1;
915 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
916 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
917 bff6ca00 2018-04-23 stsp _exit(status);
920 bff6ca00 2018-04-23 stsp static const struct got_error *
921 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
922 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
924 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
925 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
926 bff6ca00 2018-04-23 stsp int imsg_fds[2];
929 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
930 bff6ca00 2018-04-23 stsp return got_error_from_errno();
932 bff6ca00 2018-04-23 stsp pid = fork();
933 bff6ca00 2018-04-23 stsp if (pid == -1)
934 bff6ca00 2018-04-23 stsp return got_error_from_errno();
935 bff6ca00 2018-04-23 stsp else if (pid == 0) {
936 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
937 e506bf32 2018-04-23 stsp /* not reached */
940 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
941 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
942 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
943 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
944 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
945 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
946 b419fc47 2018-04-26 stsp return err ? err : err_child;
949 d1cda826 2017-11-06 stsp const struct got_error *
950 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
951 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
953 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
955 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
956 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
958 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
959 ea35256b 2018-03-16 stsp uint8_t *buf;
960 ea35256b 2018-03-16 stsp size_t len;
961 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
963 ea35256b 2018-03-16 stsp return err;
964 b29656e2 2018-03-16 stsp obj->size = len;
965 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
969 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
971 ea35256b 2018-03-16 stsp return err;
972 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
975 d1cda826 2017-11-06 stsp return err;
979 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
981 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
983 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
984 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
985 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
986 79f35eb3 2018-06-11 stsp free(qid->id);
990 59ece79d 2018-02-12 stsp free(commit->tree_id);
991 d1cda826 2017-11-06 stsp free(commit->author);
992 d1cda826 2017-11-06 stsp free(commit->committer);
993 d1cda826 2017-11-06 stsp free(commit->logmsg);
994 d1cda826 2017-11-06 stsp free(commit);
997 0ffeb3c2 2017-11-26 stsp static const struct got_error *
998 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1000 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1001 0ffeb3c2 2017-11-26 stsp size_t len;
1002 eb651edf 2018-02-11 stsp uint8_t *p;
1004 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
1005 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
1007 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
1009 0ffeb3c2 2017-11-26 stsp return err;
1011 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
1012 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1013 0ffeb3c2 2017-11-26 stsp goto done;
1016 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
1017 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
1018 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
1021 e033d803 2018-04-23 stsp return err;
1024 e033d803 2018-04-23 stsp static void
1025 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1026 e033d803 2018-04-23 stsp int imsg_fds[2])
1028 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1029 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
1030 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
1031 e033d803 2018-04-23 stsp FILE *f = NULL;
1032 e033d803 2018-04-23 stsp int status = 0;
1034 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
1035 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1036 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
1038 e033d803 2018-04-23 stsp /* revoke access to most system calls */
1039 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
1040 e033d803 2018-04-23 stsp err = got_error_from_errno();
1041 e033d803 2018-04-23 stsp goto done;
1044 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
1045 e033d803 2018-04-23 stsp if (f == NULL) {
1046 e033d803 2018-04-23 stsp err = got_error_from_errno();
1047 e033d803 2018-04-23 stsp close(obj_fd);
1048 e033d803 2018-04-23 stsp goto done;
1051 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
1053 e033d803 2018-04-23 stsp goto done;
1055 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1058 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1059 e033d803 2018-04-23 stsp if (err) {
1060 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1061 e033d803 2018-04-23 stsp status = 1;
1064 e033d803 2018-04-23 stsp fclose(f);
1065 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1066 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1067 e033d803 2018-04-23 stsp _exit(status);
1070 e033d803 2018-04-23 stsp static const struct got_error *
1071 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1074 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1075 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1076 e033d803 2018-04-23 stsp int imsg_fds[2];
1077 e033d803 2018-04-23 stsp pid_t pid;
1079 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1080 e033d803 2018-04-23 stsp return got_error_from_errno();
1082 e033d803 2018-04-23 stsp pid = fork();
1083 e033d803 2018-04-23 stsp if (pid == -1)
1084 e033d803 2018-04-23 stsp return got_error_from_errno();
1085 e033d803 2018-04-23 stsp else if (pid == 0) {
1086 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1087 e033d803 2018-04-23 stsp /* not reached */
1090 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1091 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1092 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1093 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1094 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1095 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1096 b419fc47 2018-04-26 stsp return err ? err : err_child;
1099 0ffeb3c2 2017-11-26 stsp const struct got_error *
1100 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1101 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1103 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1105 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1106 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1108 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1109 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1110 e0ab43e7 2018-03-16 stsp size_t len;
1111 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1113 e0ab43e7 2018-03-16 stsp return err;
1114 b29656e2 2018-03-16 stsp obj->size = len;
1115 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1116 e0ab43e7 2018-03-16 stsp free(buf);
1119 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1121 e0ab43e7 2018-03-16 stsp return err;
1122 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1123 d5003b79 2018-04-22 stsp close(fd);
1125 0ffeb3c2 2017-11-26 stsp return err;
1129 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1131 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1133 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1134 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1135 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1136 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1139 f715ca7f 2017-11-27 stsp free(tree);
1142 ff6b18f8 2018-04-24 stsp static const struct got_error *
1143 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1145 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1146 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1147 ff6b18f8 2018-04-24 stsp int status = 0;
1148 2967a784 2018-04-24 stsp size_t size;
1149 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1151 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1152 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1153 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1155 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1156 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1157 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1158 ff6b18f8 2018-04-24 stsp goto done;
1161 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1162 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1163 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1164 8b2180d4 2018-04-26 stsp close(infd);
1165 8b2180d4 2018-04-26 stsp goto done;
1167 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1168 8b2180d4 2018-04-26 stsp fclose(infile);
1170 ff6b18f8 2018-04-24 stsp goto done;
1172 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1174 ff6b18f8 2018-04-24 stsp if (err) {
1175 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1176 ff6b18f8 2018-04-24 stsp status = 1;
1178 ff6b18f8 2018-04-24 stsp close(outfd);
1179 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1180 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1181 ff6b18f8 2018-04-24 stsp _exit(status);
1184 ff6b18f8 2018-04-24 stsp static const struct got_error *
1185 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1187 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1188 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1189 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1190 ff6b18f8 2018-04-24 stsp pid_t pid;
1192 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1193 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1195 ff6b18f8 2018-04-24 stsp pid = fork();
1196 ff6b18f8 2018-04-24 stsp if (pid == -1)
1197 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1198 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1199 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1200 ff6b18f8 2018-04-24 stsp /* not reached */
1203 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1204 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1205 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1206 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1207 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1208 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1209 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1210 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1211 b419fc47 2018-04-26 stsp return err ? err : err_child;
1214 68482ea3 2017-11-27 stsp const struct got_error *
1215 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1216 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1218 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1220 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1221 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1223 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1224 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1226 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1227 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1228 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1230 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1231 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1232 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1233 c7254d79 2018-04-24 stsp goto done;
1235 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1236 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1238 c7254d79 2018-04-24 stsp goto done;
1240 3aca5731 2018-04-24 stsp int infd, outfd;
1241 2967a784 2018-04-24 stsp size_t size;
1242 2967a784 2018-04-24 stsp struct stat sb;
1244 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1246 c7254d79 2018-04-24 stsp goto done;
1249 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1250 3aca5731 2018-04-24 stsp if (outfd == -1) {
1251 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1252 3aca5731 2018-04-24 stsp close(infd);
1253 3aca5731 2018-04-24 stsp goto done;
1256 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1257 3aca5731 2018-04-24 stsp close(infd);
1259 c7254d79 2018-04-24 stsp goto done;
1261 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1262 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1263 2967a784 2018-04-24 stsp close(outfd);
1264 2967a784 2018-04-24 stsp goto done;
1267 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1268 2967a784 2018-04-24 stsp err = got_error_from_errno();
1269 2967a784 2018-04-24 stsp close(outfd);
1270 2967a784 2018-04-24 stsp goto done;
1273 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1274 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1275 2967a784 2018-04-24 stsp close(outfd);
1276 2967a784 2018-04-24 stsp goto done;
1279 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1280 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1281 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1282 3aca5731 2018-04-24 stsp close(outfd);
1283 3aca5731 2018-04-24 stsp goto done;
1287 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1288 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1289 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1292 c7254d79 2018-04-24 stsp if (err && *blob) {
1293 c7254d79 2018-04-24 stsp if ((*blob)->f)
1294 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1295 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1296 c7254d79 2018-04-24 stsp free(*blob);
1297 c7254d79 2018-04-24 stsp *blob = NULL;
1299 68482ea3 2017-11-27 stsp return err;
1303 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1305 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1306 68482ea3 2017-11-27 stsp fclose(blob->f);
1307 68482ea3 2017-11-27 stsp free(blob);
1311 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1313 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1317 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1319 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1322 f934cf2c 2018-02-12 stsp const uint8_t *
1323 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1325 f934cf2c 2018-02-12 stsp return blob->read_buf;
1328 68482ea3 2017-11-27 stsp const struct got_error *
1329 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1333 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1334 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1335 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1336 eb651edf 2018-02-11 stsp *outlenp = n;
1337 eb651edf 2018-02-11 stsp return NULL;