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"
49 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
52 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 ab9a70b2 2017-11-06 stsp #ifndef nitems
56 ab9a70b2 2017-11-06 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
59 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_COMMIT "commit"
60 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_TREE "tree"
61 ab9a70b2 2017-11-06 stsp #define GOT_OBJ_TAG_BLOB "blob"
63 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_TREE "tree "
64 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_PARENT "parent "
65 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_AUTHOR "author "
66 d1cda826 2017-11-06 stsp #define GOT_COMMIT_TAG_COMMITTER "committer "
68 ef0981d5 2018-02-12 stsp const struct got_error *
69 ef0981d5 2018-02-12 stsp got_object_id_str(char **outbuf, struct got_object_id *id)
71 ef0981d5 2018-02-12 stsp static const size_t len = SHA1_DIGEST_STRING_LENGTH;
73 ef0981d5 2018-02-12 stsp *outbuf = calloc(1, len);
74 ef0981d5 2018-02-12 stsp if (*outbuf == NULL)
75 0a585a0d 2018-03-17 stsp return got_error_from_errno();
77 ef0981d5 2018-02-12 stsp if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
78 ef0981d5 2018-02-12 stsp free(*outbuf);
79 ef0981d5 2018-02-12 stsp *outbuf = NULL;
80 ef0981d5 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
83 ef0981d5 2018-02-12 stsp return NULL;
87 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
89 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
92 8bf5b3c9 2018-03-17 stsp struct got_object_id *
93 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
95 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
97 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
98 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
99 8bf5b3c9 2018-03-17 stsp return NULL;
100 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
101 8bf5b3c9 2018-03-17 stsp return id2;
104 3235492e 2018-04-01 stsp struct got_object_id *
105 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
107 3235492e 2018-04-01 stsp return got_object_id_dup(&obj->id);
110 bacc9935 2018-05-20 stsp const struct got_error *
111 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
113 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
117 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
119 b107e67f 2018-01-19 stsp switch (obj->type) {
120 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
121 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
122 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
123 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
124 b107e67f 2018-01-19 stsp return obj->type;
130 96f5e8b3 2018-01-23 stsp /* not reached */
134 ab9a70b2 2017-11-06 stsp static const struct got_error *
135 d1cda826 2017-11-06 stsp parse_object_header(struct got_object **obj, char *buf, size_t len)
137 ab9a70b2 2017-11-06 stsp const char *obj_tags[] = {
138 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_COMMIT,
139 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_TREE,
140 ab9a70b2 2017-11-06 stsp GOT_OBJ_TAG_BLOB
142 ab9a70b2 2017-11-06 stsp const int obj_types[] = {
143 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_COMMIT,
144 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_TREE,
145 ab9a70b2 2017-11-06 stsp GOT_OBJ_TYPE_BLOB,
147 ab9a70b2 2017-11-06 stsp int type = 0;
148 d1cda826 2017-11-06 stsp size_t size = 0, hdrlen = 0;
150 ab9a70b2 2017-11-06 stsp char *p = strchr(buf, '\0');
152 ab9a70b2 2017-11-06 stsp if (p == NULL)
153 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
155 d1cda826 2017-11-06 stsp hdrlen = strlen(buf) + 1 /* '\0' */;
157 ab9a70b2 2017-11-06 stsp for (i = 0; i < nitems(obj_tags); i++) {
158 ab9a70b2 2017-11-06 stsp const char *tag = obj_tags[i];
159 63323519 2017-11-06 stsp size_t tlen = strlen(tag);
160 ab9a70b2 2017-11-06 stsp const char *errstr;
162 63323519 2017-11-06 stsp if (strncmp(buf, tag, tlen) != 0)
165 ab9a70b2 2017-11-06 stsp type = obj_types[i];
166 63323519 2017-11-06 stsp if (len <= tlen)
167 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
168 63323519 2017-11-06 stsp size = strtonum(buf + tlen, 0, LONG_MAX, &errstr);
169 ab9a70b2 2017-11-06 stsp if (errstr != NULL)
170 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
174 ab9a70b2 2017-11-06 stsp if (type == 0)
175 ab9a70b2 2017-11-06 stsp return got_error(GOT_ERR_BAD_OBJ_HDR);
177 ab9a70b2 2017-11-06 stsp *obj = calloc(1, sizeof(**obj));
178 1db76ab5 2018-01-26 mpi if (*obj == NULL)
179 0a585a0d 2018-03-17 stsp return got_error_from_errno();
180 ab9a70b2 2017-11-06 stsp (*obj)->type = type;
181 d1cda826 2017-11-06 stsp (*obj)->hdrlen = hdrlen;
182 ab9a70b2 2017-11-06 stsp (*obj)->size = size;
183 ab9a70b2 2017-11-06 stsp return NULL;
186 ab9a70b2 2017-11-06 stsp static const struct got_error *
187 2178c42e 2018-04-22 stsp read_object_header(struct got_object **obj, FILE *f)
189 ab9a70b2 2017-11-06 stsp const struct got_error *err;
190 ab9a70b2 2017-11-06 stsp struct got_zstream_buf zb;
192 a3e2cbea 2017-12-01 stsp const size_t zbsize = 64;
193 744d9326 2017-12-01 stsp size_t outlen, totlen;
196 744d9326 2017-12-01 stsp buf = calloc(zbsize, sizeof(char));
197 a3e2cbea 2017-12-01 stsp if (buf == NULL)
198 0a585a0d 2018-03-17 stsp return got_error_from_errno();
200 19d747f7 2018-03-16 stsp err = got_inflate_init(&zb, NULL, zbsize);
202 ab9a70b2 2017-11-06 stsp return err;
205 744d9326 2017-12-01 stsp totlen = 0;
207 126ee060 2018-02-11 stsp err = got_inflate_read(&zb, f, &outlen);
210 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
211 a3e2cbea 2017-12-01 stsp buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
212 e302c59e 2017-12-01 stsp if (buf == NULL) {
213 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
217 c56976de 2017-12-01 stsp memcpy(buf + totlen, zb.outbuf, outlen);
218 744d9326 2017-12-01 stsp totlen += outlen;
220 a3e2cbea 2017-12-01 stsp } while (strchr(zb.outbuf, '\0') == NULL);
222 744d9326 2017-12-01 stsp err = parse_object_header(obj, buf, totlen);
224 4ca7b755 2018-01-26 stsp got_inflate_end(&zb);
225 2178c42e 2018-04-22 stsp return err;
228 302b7dd6 2018-04-23 stsp static void
229 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(int obj_fd, int imsg_fds[2])
231 302b7dd6 2018-04-23 stsp const struct got_error *err = NULL;
232 e3306bd9 2018-04-23 stsp struct got_object *obj = NULL;
233 e3306bd9 2018-04-23 stsp struct imsgbuf ibuf;
234 302b7dd6 2018-04-23 stsp FILE *f = NULL;
235 302b7dd6 2018-04-23 stsp int status = 0;
237 be37c2e6 2018-04-24 stsp setproctitle("read object header");
238 302b7dd6 2018-04-23 stsp close(imsg_fds[0]);
239 e3306bd9 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
241 302b7dd6 2018-04-23 stsp /* revoke access to most system calls */
242 302b7dd6 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
243 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
247 302b7dd6 2018-04-23 stsp f = fdopen(obj_fd, "rb");
248 302b7dd6 2018-04-23 stsp if (f == NULL) {
249 302b7dd6 2018-04-23 stsp err = got_error_from_errno();
250 302b7dd6 2018-04-23 stsp close(obj_fd);
254 e3306bd9 2018-04-23 stsp err = read_object_header(&obj, f);
258 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
261 e3306bd9 2018-04-23 stsp got_object_close(obj);
263 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
264 302b7dd6 2018-04-23 stsp status = 1;
268 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
269 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
270 302b7dd6 2018-04-23 stsp _exit(status);
273 b419fc47 2018-04-26 stsp static const struct got_error *
274 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
276 b419fc47 2018-04-26 stsp int child_status;
278 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
280 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
281 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
283 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
284 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
286 b419fc47 2018-04-26 stsp return NULL;
289 2178c42e 2018-04-22 stsp static const struct got_error *
290 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
292 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
293 2178c42e 2018-04-22 stsp int imsg_fds[2];
294 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
297 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
298 2178c42e 2018-04-22 stsp return got_error_from_errno();
300 2178c42e 2018-04-22 stsp pid = fork();
301 2178c42e 2018-04-22 stsp if (pid == -1)
302 2178c42e 2018-04-22 stsp return got_error_from_errno();
303 2178c42e 2018-04-22 stsp else if (pid == 0) {
304 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
305 302b7dd6 2018-04-23 stsp /* not reached */
308 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
309 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
310 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
311 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
312 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
313 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
314 b419fc47 2018-04-26 stsp return err ? err : err_child;
317 d1cda826 2017-11-06 stsp static const struct got_error *
318 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
320 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
322 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
324 e6b1056e 2018-04-22 stsp *path = NULL;
326 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
327 0a585a0d 2018-03-17 stsp return got_error_from_errno();
329 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
331 ef0981d5 2018-02-12 stsp return err;
333 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
334 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
335 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
338 d1cda826 2017-11-06 stsp free(path_objects);
339 d1cda826 2017-11-06 stsp return err;
342 4ee4114f 2018-01-23 stsp static const struct got_error *
343 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
345 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
346 a1fd68d8 2018-01-12 stsp char *path;
348 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
350 d1cda826 2017-11-06 stsp return err;
351 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
352 d5003b79 2018-04-22 stsp if (*fd == -1) {
353 6c00b545 2018-01-17 stsp err = got_error_from_errno();
357 4558fcd4 2018-01-14 stsp free(path);
358 4558fcd4 2018-01-14 stsp return err;
361 4558fcd4 2018-01-14 stsp const struct got_error *
362 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
363 4558fcd4 2018-01-14 stsp struct got_object_id *id)
365 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
366 6c00b545 2018-01-17 stsp char *path;
369 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
370 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
371 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
372 7bb0daa1 2018-06-21 stsp return NULL;
375 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
377 4558fcd4 2018-01-14 stsp return err;
379 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
380 2178c42e 2018-04-22 stsp if (fd == -1) {
381 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
382 6c00b545 2018-01-17 stsp err = got_error_from_errno();
385 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
388 6c00b545 2018-01-17 stsp if (*obj == NULL)
389 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
391 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
394 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
397 7bb0daa1 2018-06-21 stsp if (err == NULL) {
398 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
399 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
402 6c00b545 2018-01-17 stsp free(path);
403 2178c42e 2018-04-22 stsp if (fd != -1)
405 ab9a70b2 2017-11-06 stsp return err;
409 6dfa2fd3 2018-02-12 stsp const struct got_error *
410 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
411 6dfa2fd3 2018-02-12 stsp const char *id_str)
413 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
415 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
416 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
418 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
422 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
424 7bb0daa1 2018-06-21 stsp if (obj->refcnt > 0) {
425 7bb0daa1 2018-06-21 stsp obj->refcnt--;
429 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
430 c3703302 2018-01-23 stsp struct got_delta *delta;
431 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
432 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
433 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
434 c3703302 2018-01-23 stsp got_delta_close(delta);
437 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
438 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
442 bff6ca00 2018-04-23 stsp struct got_commit_object *
443 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
445 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
447 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
448 bff6ca00 2018-04-23 stsp if (commit == NULL)
449 bff6ca00 2018-04-23 stsp return NULL;
450 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
451 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
452 bff6ca00 2018-04-23 stsp free(commit);
453 bff6ca00 2018-04-23 stsp return NULL;
456 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
458 bff6ca00 2018-04-23 stsp return commit;
461 bff6ca00 2018-04-23 stsp const struct got_error *
462 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
463 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
465 be6a1b5a 2018-06-11 stsp const struct got_error *err;
466 be6a1b5a 2018-06-11 stsp struct got_object *obj;
468 835e0dbd 2018-06-21 stsp *commit = NULL;
470 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
472 be6a1b5a 2018-06-11 stsp return err;
473 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
474 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
478 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
480 be6a1b5a 2018-06-11 stsp got_object_close(obj);
481 be6a1b5a 2018-06-11 stsp return err;
484 be6a1b5a 2018-06-11 stsp const struct got_error *
485 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
486 bff6ca00 2018-04-23 stsp const char *id_str)
488 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
489 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
491 79f35eb3 2018-06-11 stsp qid = calloc(1, sizeof(*qid));
492 79f35eb3 2018-06-11 stsp if (qid == NULL)
493 bff6ca00 2018-04-23 stsp return got_error_from_errno();
495 79f35eb3 2018-06-11 stsp qid->id = calloc(1, sizeof(*qid->id));
496 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
497 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
499 bff6ca00 2018-04-23 stsp return err;
502 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
503 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
504 79f35eb3 2018-06-11 stsp free(qid->id);
506 bff6ca00 2018-04-23 stsp return err;
509 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
510 bff6ca00 2018-04-23 stsp commit->nparents++;
512 4626e416 2018-06-10 stsp return NULL;
515 4626e416 2018-06-10 stsp static const struct got_error *
516 788c352e 2018-06-16 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
518 788c352e 2018-06-16 stsp int sign = 1;
519 788c352e 2018-06-16 stsp const char *p = tzstr;
520 788c352e 2018-06-16 stsp time_t h, m;
522 788c352e 2018-06-16 stsp *gmtoff = 0;
524 788c352e 2018-06-16 stsp if (*p == '-')
526 788c352e 2018-06-16 stsp else if (*p != '+')
527 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
529 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
530 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
531 788c352e 2018-06-16 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
534 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
535 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
536 788c352e 2018-06-16 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
538 788c352e 2018-06-16 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
539 788c352e 2018-06-16 stsp return NULL;
542 788c352e 2018-06-16 stsp static const struct got_error *
543 788c352e 2018-06-16 stsp parse_commit_time(struct tm *tm, char *committer)
545 788c352e 2018-06-16 stsp const struct got_error *err = NULL;
546 788c352e 2018-06-16 stsp const char *errstr;
547 788c352e 2018-06-16 stsp char *space, *tzstr;
548 788c352e 2018-06-16 stsp time_t gmtoff;
549 788c352e 2018-06-16 stsp time_t time;
551 788c352e 2018-06-16 stsp /* Parse and strip off trailing timezone indicator string. */
552 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
553 4626e416 2018-06-10 stsp if (space == NULL)
554 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
555 788c352e 2018-06-16 stsp tzstr = strdup(space + 1);
556 788c352e 2018-06-16 stsp if (tzstr == NULL)
557 6c281f94 2018-06-11 stsp return got_error_from_errno();
558 788c352e 2018-06-16 stsp err = parse_gmtoff(&gmtoff, tzstr);
559 788c352e 2018-06-16 stsp free(tzstr);
561 788c352e 2018-06-16 stsp return err;
562 4626e416 2018-06-10 stsp *space = '\0';
564 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
565 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
566 4626e416 2018-06-10 stsp if (space == NULL)
567 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
569 788c352e 2018-06-16 stsp /* Timestamp parsed here is expressed in comitter's local time. */
570 788c352e 2018-06-16 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
571 4626e416 2018-06-10 stsp if (errstr)
572 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
574 788c352e 2018-06-16 stsp /* Express the time stamp in UTC. */
575 788c352e 2018-06-16 stsp memset(tm, 0, sizeof(*tm));
576 788c352e 2018-06-16 stsp time -= gmtoff;
577 788c352e 2018-06-16 stsp if (localtime_r(&time, tm) == NULL)
578 788c352e 2018-06-16 stsp return got_error_from_errno();
579 788c352e 2018-06-16 stsp tm->tm_gmtoff = gmtoff;
581 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
582 4626e416 2018-06-10 stsp *space = '\0';
584 bff6ca00 2018-04-23 stsp return NULL;
587 d1cda826 2017-11-06 stsp static const struct got_error *
588 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
590 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
591 d1cda826 2017-11-06 stsp char *s = buf;
592 d1cda826 2017-11-06 stsp size_t tlen;
593 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
595 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
596 d1cda826 2017-11-06 stsp if (*commit == NULL)
597 0a585a0d 2018-03-17 stsp return got_error_from_errno();
599 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
600 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, 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 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
608 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
611 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
612 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
614 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
618 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
619 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
620 d1cda826 2017-11-06 stsp remain -= tlen;
621 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
622 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
626 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
630 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
631 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
634 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
635 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
637 4626e416 2018-06-10 stsp size_t slen;
639 d1cda826 2017-11-06 stsp remain -= tlen;
640 d1cda826 2017-11-06 stsp if (remain <= 0) {
641 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
646 d1cda826 2017-11-06 stsp if (p == NULL) {
647 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
651 4626e416 2018-06-10 stsp slen = strlen(s);
652 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_author, s);
655 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
656 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
657 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
660 4626e416 2018-06-10 stsp s += slen + 1;
661 4626e416 2018-06-10 stsp remain -= slen + 1;
664 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
665 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
667 4626e416 2018-06-10 stsp size_t slen;
669 d1cda826 2017-11-06 stsp remain -= tlen;
670 d1cda826 2017-11-06 stsp if (remain <= 0) {
671 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
675 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
676 d1cda826 2017-11-06 stsp if (p == NULL) {
677 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
681 4626e416 2018-06-10 stsp slen = strlen(s);
682 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
685 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
686 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
687 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
690 4626e416 2018-06-10 stsp s += slen + 1;
691 4626e416 2018-06-10 stsp remain -= slen + 1;
694 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
695 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
696 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
701 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
702 59ece79d 2018-02-12 stsp *commit = NULL;
704 0ffeb3c2 2017-11-26 stsp return err;
707 0ffeb3c2 2017-11-26 stsp static void
708 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
710 59ece79d 2018-02-12 stsp free(te->id);
711 0ffeb3c2 2017-11-26 stsp free(te->name);
715 e033d803 2018-04-23 stsp struct got_tree_entry *
716 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
718 e033d803 2018-04-23 stsp struct got_tree_entry *te;
720 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
721 e033d803 2018-04-23 stsp if (te == NULL)
722 e033d803 2018-04-23 stsp return NULL;
724 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
725 e033d803 2018-04-23 stsp if (te->id == NULL) {
732 0ffeb3c2 2017-11-26 stsp static const struct got_error *
733 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
734 0ffeb3c2 2017-11-26 stsp size_t maxlen)
736 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
737 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
739 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
740 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
741 0a585a0d 2018-03-17 stsp return got_error_from_errno();
743 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
744 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
746 59ece79d 2018-02-12 stsp *te = NULL;
747 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
750 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
751 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
752 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
754 59ece79d 2018-02-12 stsp *te = NULL;
755 0a585a0d 2018-03-17 stsp return err;
757 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
758 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
759 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
762 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
763 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
767 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
768 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
769 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
772 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
773 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
774 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
777 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
778 59ece79d 2018-02-12 stsp *te = NULL;
780 d1cda826 2017-11-06 stsp return err;
783 d1cda826 2017-11-06 stsp static const struct got_error *
784 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
786 90356acc 2018-01-27 stsp const struct got_error *err;
787 0ffeb3c2 2017-11-26 stsp size_t remain = len;
789 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
790 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
791 0a585a0d 2018-03-17 stsp return got_error_from_errno();
793 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INIT(&(*tree)->entries);
795 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
796 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
797 0ffeb3c2 2017-11-26 stsp size_t elen;
799 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
801 90356acc 2018-01-27 stsp return err;
802 0ffeb3c2 2017-11-26 stsp (*tree)->nentries++;
803 0ffeb3c2 2017-11-26 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries, te, entry);
804 0ffeb3c2 2017-11-26 stsp buf += elen;
805 0ffeb3c2 2017-11-26 stsp remain -= elen;
808 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
809 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
810 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
813 0ffeb3c2 2017-11-26 stsp return NULL;
816 0ffeb3c2 2017-11-26 stsp static const struct got_error *
817 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
819 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
820 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
821 eb651edf 2018-02-11 stsp size_t n, total, remain;
822 eb651edf 2018-02-11 stsp uint8_t *buf;
824 eb651edf 2018-02-11 stsp *outbuf = NULL;
825 eb651edf 2018-02-11 stsp *outlen = 0;
827 eb651edf 2018-02-11 stsp buf = calloc(1, blocksize);
828 eb651edf 2018-02-11 stsp if (buf == NULL)
829 0a585a0d 2018-03-17 stsp return got_error_from_errno();
831 eb651edf 2018-02-11 stsp remain = blocksize;
833 eb651edf 2018-02-11 stsp while (1) {
834 eb651edf 2018-02-11 stsp if (remain == 0) {
835 eb651edf 2018-02-11 stsp uint8_t *newbuf;
836 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
837 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
838 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
841 eb651edf 2018-02-11 stsp buf = newbuf;
842 eb651edf 2018-02-11 stsp remain += blocksize;
844 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
845 eb651edf 2018-02-11 stsp if (n == 0) {
846 eb651edf 2018-02-11 stsp if (ferror(f)) {
847 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
850 eb651edf 2018-02-11 stsp break; /* EOF */
852 eb651edf 2018-02-11 stsp remain -= n;
853 eb651edf 2018-02-11 stsp total += n;
857 eb651edf 2018-02-11 stsp if (err == NULL) {
858 eb651edf 2018-02-11 stsp *outbuf = buf;
859 eb651edf 2018-02-11 stsp *outlen = total;
862 eb651edf 2018-02-11 stsp return err;
865 eb651edf 2018-02-11 stsp static const struct got_error *
866 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
869 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
870 d1cda826 2017-11-06 stsp size_t len;
871 eb651edf 2018-02-11 stsp uint8_t *p;
873 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
874 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
876 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
878 d1cda826 2017-11-06 stsp return err;
880 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
881 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
885 d1cda826 2017-11-06 stsp /* Skip object header. */
886 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
887 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
890 d1cda826 2017-11-06 stsp return err;
893 bff6ca00 2018-04-23 stsp static void
894 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
895 bff6ca00 2018-04-23 stsp int imsg_fds[2])
897 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
898 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
899 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
900 bff6ca00 2018-04-23 stsp FILE *f = NULL;
901 bff6ca00 2018-04-23 stsp int status = 0;
903 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
904 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
905 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
907 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
908 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
909 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
913 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
914 bff6ca00 2018-04-23 stsp if (f == NULL) {
915 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
916 bff6ca00 2018-04-23 stsp close(obj_fd);
920 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
924 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
926 bff6ca00 2018-04-23 stsp if (commit)
927 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
929 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
930 bff6ca00 2018-04-23 stsp status = 1;
934 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
935 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
936 bff6ca00 2018-04-23 stsp _exit(status);
939 bff6ca00 2018-04-23 stsp static const struct got_error *
940 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
941 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
943 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
944 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
945 bff6ca00 2018-04-23 stsp int imsg_fds[2];
948 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
949 bff6ca00 2018-04-23 stsp return got_error_from_errno();
951 bff6ca00 2018-04-23 stsp pid = fork();
952 bff6ca00 2018-04-23 stsp if (pid == -1)
953 bff6ca00 2018-04-23 stsp return got_error_from_errno();
954 bff6ca00 2018-04-23 stsp else if (pid == 0) {
955 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
956 e506bf32 2018-04-23 stsp /* not reached */
959 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
960 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
961 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
962 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
963 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
964 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
965 b419fc47 2018-04-26 stsp return err ? err : err_child;
968 d1cda826 2017-11-06 stsp const struct got_error *
969 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
970 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
972 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
974 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
975 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
977 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
978 ea35256b 2018-03-16 stsp uint8_t *buf;
979 ea35256b 2018-03-16 stsp size_t len;
980 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
982 ea35256b 2018-03-16 stsp return err;
983 b29656e2 2018-03-16 stsp obj->size = len;
984 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
988 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
990 ea35256b 2018-03-16 stsp return err;
991 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
994 d1cda826 2017-11-06 stsp return err;
998 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
1000 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1002 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1003 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
1004 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1005 79f35eb3 2018-06-11 stsp free(qid->id);
1006 79f35eb3 2018-06-11 stsp free(qid);
1009 59ece79d 2018-02-12 stsp free(commit->tree_id);
1010 d1cda826 2017-11-06 stsp free(commit->author);
1011 d1cda826 2017-11-06 stsp free(commit->committer);
1012 d1cda826 2017-11-06 stsp free(commit->logmsg);
1013 d1cda826 2017-11-06 stsp free(commit);
1016 0ffeb3c2 2017-11-26 stsp static const struct got_error *
1017 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1019 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1020 0ffeb3c2 2017-11-26 stsp size_t len;
1021 eb651edf 2018-02-11 stsp uint8_t *p;
1023 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
1024 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
1026 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
1028 0ffeb3c2 2017-11-26 stsp return err;
1030 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
1031 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1032 0ffeb3c2 2017-11-26 stsp goto done;
1035 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
1036 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
1037 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
1040 e033d803 2018-04-23 stsp return err;
1043 e033d803 2018-04-23 stsp static void
1044 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1045 e033d803 2018-04-23 stsp int imsg_fds[2])
1047 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1048 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
1049 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
1050 e033d803 2018-04-23 stsp FILE *f = NULL;
1051 e033d803 2018-04-23 stsp int status = 0;
1053 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
1054 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1055 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
1057 e033d803 2018-04-23 stsp /* revoke access to most system calls */
1058 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
1059 e033d803 2018-04-23 stsp err = got_error_from_errno();
1060 e033d803 2018-04-23 stsp goto done;
1063 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
1064 e033d803 2018-04-23 stsp if (f == NULL) {
1065 e033d803 2018-04-23 stsp err = got_error_from_errno();
1066 e033d803 2018-04-23 stsp close(obj_fd);
1067 e033d803 2018-04-23 stsp goto done;
1070 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
1072 e033d803 2018-04-23 stsp goto done;
1074 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1077 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1078 e033d803 2018-04-23 stsp if (err) {
1079 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1080 e033d803 2018-04-23 stsp status = 1;
1083 e033d803 2018-04-23 stsp fclose(f);
1084 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1085 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1086 e033d803 2018-04-23 stsp _exit(status);
1089 e033d803 2018-04-23 stsp static const struct got_error *
1090 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1093 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1094 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1095 e033d803 2018-04-23 stsp int imsg_fds[2];
1096 e033d803 2018-04-23 stsp pid_t pid;
1098 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1099 e033d803 2018-04-23 stsp return got_error_from_errno();
1101 e033d803 2018-04-23 stsp pid = fork();
1102 e033d803 2018-04-23 stsp if (pid == -1)
1103 e033d803 2018-04-23 stsp return got_error_from_errno();
1104 e033d803 2018-04-23 stsp else if (pid == 0) {
1105 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1106 e033d803 2018-04-23 stsp /* not reached */
1109 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1110 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1111 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1112 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1113 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1114 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1115 b419fc47 2018-04-26 stsp return err ? err : err_child;
1118 0ffeb3c2 2017-11-26 stsp const struct got_error *
1119 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1120 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1122 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1124 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1125 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1127 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1128 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1129 e0ab43e7 2018-03-16 stsp size_t len;
1130 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1132 e0ab43e7 2018-03-16 stsp return err;
1133 b29656e2 2018-03-16 stsp obj->size = len;
1134 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1135 e0ab43e7 2018-03-16 stsp free(buf);
1138 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1140 e0ab43e7 2018-03-16 stsp return err;
1141 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1142 d5003b79 2018-04-22 stsp close(fd);
1144 776d4d29 2018-06-17 stsp return err;
1147 776d4d29 2018-06-17 stsp const struct got_error *
1148 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1149 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1151 776d4d29 2018-06-17 stsp const struct got_error *err;
1152 776d4d29 2018-06-17 stsp struct got_object *obj;
1154 835e0dbd 2018-06-21 stsp *tree = NULL;
1156 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
1158 776d4d29 2018-06-17 stsp return err;
1159 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1160 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1161 776d4d29 2018-06-17 stsp goto done;
1164 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
1166 776d4d29 2018-06-17 stsp got_object_close(obj);
1167 0ffeb3c2 2017-11-26 stsp return err;
1171 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1173 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1175 f715ca7f 2017-11-27 stsp while (!SIMPLEQ_EMPTY(&tree->entries)) {
1176 f715ca7f 2017-11-27 stsp te = SIMPLEQ_FIRST(&tree->entries);
1177 f715ca7f 2017-11-27 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries, entry);
1178 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1181 f715ca7f 2017-11-27 stsp free(tree);
1184 ff6b18f8 2018-04-24 stsp static const struct got_error *
1185 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1187 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1188 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1189 ff6b18f8 2018-04-24 stsp int status = 0;
1190 2967a784 2018-04-24 stsp size_t size;
1191 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1193 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1194 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1195 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1197 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1198 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1199 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1200 ff6b18f8 2018-04-24 stsp goto done;
1203 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1204 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1205 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1206 8b2180d4 2018-04-26 stsp close(infd);
1207 8b2180d4 2018-04-26 stsp goto done;
1209 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1210 8b2180d4 2018-04-26 stsp fclose(infile);
1212 ff6b18f8 2018-04-24 stsp goto done;
1214 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1216 ff6b18f8 2018-04-24 stsp if (err) {
1217 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1218 ff6b18f8 2018-04-24 stsp status = 1;
1220 ff6b18f8 2018-04-24 stsp close(outfd);
1221 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1222 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1223 ff6b18f8 2018-04-24 stsp _exit(status);
1226 ff6b18f8 2018-04-24 stsp static const struct got_error *
1227 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1229 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1230 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1231 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1232 ff6b18f8 2018-04-24 stsp pid_t pid;
1234 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1235 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1237 ff6b18f8 2018-04-24 stsp pid = fork();
1238 ff6b18f8 2018-04-24 stsp if (pid == -1)
1239 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1240 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1241 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1242 ff6b18f8 2018-04-24 stsp /* not reached */
1245 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1246 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1247 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1248 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1249 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1250 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1251 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1252 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1253 b419fc47 2018-04-26 stsp return err ? err : err_child;
1256 68482ea3 2017-11-27 stsp const struct got_error *
1257 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1258 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1260 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1262 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1263 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1265 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1266 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1268 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1269 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1270 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1272 15c8b0e6 2018-04-24 stsp (*blob)->read_buf = calloc(1, blocksize);
1273 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1274 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1275 c7254d79 2018-04-24 stsp goto done;
1277 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1278 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1280 c7254d79 2018-04-24 stsp goto done;
1282 3aca5731 2018-04-24 stsp int infd, outfd;
1283 2967a784 2018-04-24 stsp size_t size;
1284 2967a784 2018-04-24 stsp struct stat sb;
1286 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1288 c7254d79 2018-04-24 stsp goto done;
1291 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1292 3aca5731 2018-04-24 stsp if (outfd == -1) {
1293 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1294 3aca5731 2018-04-24 stsp close(infd);
1295 3aca5731 2018-04-24 stsp goto done;
1298 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1299 3aca5731 2018-04-24 stsp close(infd);
1301 c7254d79 2018-04-24 stsp goto done;
1303 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1304 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1305 2967a784 2018-04-24 stsp close(outfd);
1306 2967a784 2018-04-24 stsp goto done;
1309 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1310 2967a784 2018-04-24 stsp err = got_error_from_errno();
1311 2967a784 2018-04-24 stsp close(outfd);
1312 2967a784 2018-04-24 stsp goto done;
1315 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1316 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1317 2967a784 2018-04-24 stsp close(outfd);
1318 2967a784 2018-04-24 stsp goto done;
1321 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1322 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1323 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1324 3aca5731 2018-04-24 stsp close(outfd);
1325 3aca5731 2018-04-24 stsp goto done;
1329 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1330 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1331 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1334 c7254d79 2018-04-24 stsp if (err && *blob) {
1335 c7254d79 2018-04-24 stsp if ((*blob)->f)
1336 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1337 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1338 c7254d79 2018-04-24 stsp free(*blob);
1339 c7254d79 2018-04-24 stsp *blob = NULL;
1341 a19581a2 2018-06-21 stsp return err;
1344 a19581a2 2018-06-21 stsp const struct got_error *
1345 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1346 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1347 a19581a2 2018-06-21 stsp size_t blocksize)
1349 a19581a2 2018-06-21 stsp const struct got_error *err;
1350 a19581a2 2018-06-21 stsp struct got_object *obj;
1352 835e0dbd 2018-06-21 stsp *blob = NULL;
1354 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
1356 a19581a2 2018-06-21 stsp return err;
1357 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1358 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1359 a19581a2 2018-06-21 stsp goto done;
1362 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
1364 a19581a2 2018-06-21 stsp got_object_close(obj);
1365 68482ea3 2017-11-27 stsp return err;
1369 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1371 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1372 68482ea3 2017-11-27 stsp fclose(blob->f);
1373 68482ea3 2017-11-27 stsp free(blob);
1377 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1379 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1383 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1385 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1388 f934cf2c 2018-02-12 stsp const uint8_t *
1389 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1391 f934cf2c 2018-02-12 stsp return blob->read_buf;
1394 68482ea3 2017-11-27 stsp const struct got_error *
1395 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1399 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1400 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1401 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1402 eb651edf 2018-02-11 stsp *outlenp = n;
1403 35e9ba5d 2018-06-21 stsp return NULL;
1406 35e9ba5d 2018-06-21 stsp const struct got_error *
1407 35e9ba5d 2018-06-21 stsp got_object_blob_dump_to_file(size_t *total_len, FILE *outfile,
1408 35e9ba5d 2018-06-21 stsp struct got_blob_object *blob)
1410 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1411 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
1413 35e9ba5d 2018-06-21 stsp *total_len = 0;
1414 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1416 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1418 35e9ba5d 2018-06-21 stsp return err;
1419 35e9ba5d 2018-06-21 stsp if (len == 0)
1421 35e9ba5d 2018-06-21 stsp *total_len += len;
1422 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1423 35e9ba5d 2018-06-21 stsp fwrite(got_object_blob_get_read_buf(blob) + hdrlen,
1424 35e9ba5d 2018-06-21 stsp len - hdrlen, 1, outfile);
1425 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1426 35e9ba5d 2018-06-21 stsp } while (len != 0);
1428 35e9ba5d 2018-06-21 stsp fflush(outfile);
1429 35e9ba5d 2018-06-21 stsp rewind(outfile);
1431 776d4d29 2018-06-17 stsp return NULL;
1434 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1435 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
1437 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1439 776d4d29 2018-06-17 stsp SIMPLEQ_FOREACH(te, &tree->entries, entry) {
1440 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
1441 776d4d29 2018-06-17 stsp return te;
1443 eb651edf 2018-02-11 stsp return NULL;
1446 776d4d29 2018-06-17 stsp const struct got_error *
1447 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1448 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1450 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1451 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1452 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1453 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1454 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
1455 67606321 2018-06-21 stsp size_t len = strlen(path);
1457 776d4d29 2018-06-17 stsp *obj = NULL;
1459 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1460 776d4d29 2018-06-17 stsp if (path[0] != '/')
1461 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1463 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1465 776d4d29 2018-06-17 stsp goto done;
1467 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1468 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1469 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
1471 776d4d29 2018-06-17 stsp goto done;
1472 776d4d29 2018-06-17 stsp return NULL;
1475 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1477 776d4d29 2018-06-17 stsp goto done;
1479 197aa481 2018-06-21 stsp s0 = strdup(path);
1480 197aa481 2018-06-21 stsp if (s0 == NULL) {
1481 776d4d29 2018-06-17 stsp err = got_error_from_errno();
1482 776d4d29 2018-06-17 stsp goto done;
1484 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
1486 776d4d29 2018-06-17 stsp goto done;
1489 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1492 67606321 2018-06-21 stsp while (len > 0) {
1493 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1495 776d4d29 2018-06-17 stsp if (*s != '/') {
1502 776d4d29 2018-06-17 stsp /* end of path segment */
1503 776d4d29 2018-06-17 stsp *s = '\0';
1505 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
1506 db37e2c0 2018-06-21 stsp if (te == NULL) {
1507 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
1508 776d4d29 2018-06-17 stsp goto done;
1511 67606321 2018-06-21 stsp if (len == 0)
1514 776d4d29 2018-06-17 stsp seg = s + 1;
1518 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1520 db37e2c0 2018-06-21 stsp te = NULL;
1522 776d4d29 2018-06-17 stsp goto done;
1523 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1524 776d4d29 2018-06-17 stsp tree = next_tree;
1529 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
1531 7bb0daa1 2018-06-21 stsp goto done;
1533 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
1536 776d4d29 2018-06-17 stsp if (commit)
1537 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1539 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1540 776d4d29 2018-06-17 stsp return err;