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 63581804 2018-07-09 stsp #include "got_lib_inflate.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 062ebb78 2018-07-12 stsp *outbuf = malloc(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 4842267f 2018-06-22 stsp read_object_header(struct got_object **obj, int fd)
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 062ebb78 2018-07-12 stsp buf = malloc(zbsize);
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 4842267f 2018-06-22 stsp err = got_inflate_read_fd(&zb, fd, &outlen);
210 e302c59e 2017-12-01 stsp if (strchr(zb.outbuf, '\0') == NULL) {
211 062ebb78 2018-07-12 stsp buf = reallocarray(buf, 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 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 4842267f 2018-06-22 stsp err = read_object_header(&obj, obj_fd);
250 e3306bd9 2018-04-23 stsp err = got_privsep_send_obj(&ibuf, obj, 0);
253 e3306bd9 2018-04-23 stsp got_object_close(obj);
255 e3306bd9 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
256 302b7dd6 2018-04-23 stsp status = 1;
258 4842267f 2018-06-22 stsp close(obj_fd);
259 e3306bd9 2018-04-23 stsp imsg_clear(&ibuf);
260 302b7dd6 2018-04-23 stsp close(imsg_fds[1]);
261 302b7dd6 2018-04-23 stsp _exit(status);
264 b419fc47 2018-04-26 stsp static const struct got_error *
265 b419fc47 2018-04-26 stsp wait_for_child(pid_t pid)
267 b419fc47 2018-04-26 stsp int child_status;
269 b419fc47 2018-04-26 stsp waitpid(pid, &child_status, 0);
271 b419fc47 2018-04-26 stsp if (!WIFEXITED(child_status))
272 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_DIED);
274 b419fc47 2018-04-26 stsp if (WEXITSTATUS(child_status) != 0)
275 b419fc47 2018-04-26 stsp return got_error(GOT_ERR_PRIVSEP_EXIT);
277 b419fc47 2018-04-26 stsp return NULL;
280 2178c42e 2018-04-22 stsp static const struct got_error *
281 2178c42e 2018-04-22 stsp read_object_header_privsep(struct got_object **obj, int fd)
283 2178c42e 2018-04-22 stsp struct imsgbuf parent_ibuf;
284 2178c42e 2018-04-22 stsp int imsg_fds[2];
285 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
288 2178c42e 2018-04-22 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
289 2178c42e 2018-04-22 stsp return got_error_from_errno();
291 2178c42e 2018-04-22 stsp pid = fork();
292 2178c42e 2018-04-22 stsp if (pid == -1)
293 2178c42e 2018-04-22 stsp return got_error_from_errno();
294 2178c42e 2018-04-22 stsp else if (pid == 0) {
295 302b7dd6 2018-04-23 stsp read_object_header_privsep_child(fd, imsg_fds);
296 302b7dd6 2018-04-23 stsp /* not reached */
299 2178c42e 2018-04-22 stsp close(imsg_fds[1]);
300 2178c42e 2018-04-22 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
301 2178c42e 2018-04-22 stsp err = got_privsep_recv_obj(obj, &parent_ibuf);
302 2178c42e 2018-04-22 stsp imsg_clear(&parent_ibuf);
303 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
304 2178c42e 2018-04-22 stsp close(imsg_fds[0]);
305 b419fc47 2018-04-26 stsp return err ? err : err_child;
308 d1cda826 2017-11-06 stsp static const struct got_error *
309 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
311 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
313 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
315 e6b1056e 2018-04-22 stsp *path = NULL;
317 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
318 0a585a0d 2018-03-17 stsp return got_error_from_errno();
320 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
322 ef0981d5 2018-02-12 stsp return err;
324 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
325 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
326 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
329 d1cda826 2017-11-06 stsp free(path_objects);
330 d1cda826 2017-11-06 stsp return err;
333 4ee4114f 2018-01-23 stsp static const struct got_error *
334 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
336 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
337 a1fd68d8 2018-01-12 stsp char *path;
339 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
341 d1cda826 2017-11-06 stsp return err;
342 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
343 d5003b79 2018-04-22 stsp if (*fd == -1) {
344 6c00b545 2018-01-17 stsp err = got_error_from_errno();
348 4558fcd4 2018-01-14 stsp free(path);
349 4558fcd4 2018-01-14 stsp return err;
352 4558fcd4 2018-01-14 stsp const struct got_error *
353 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
354 4558fcd4 2018-01-14 stsp struct got_object_id *id)
356 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
357 6c00b545 2018-01-17 stsp char *path;
360 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
361 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
362 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
363 7bb0daa1 2018-06-21 stsp return NULL;
366 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
368 4558fcd4 2018-01-14 stsp return err;
370 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
371 2178c42e 2018-04-22 stsp if (fd == -1) {
372 6c00b545 2018-01-17 stsp if (errno != ENOENT) {
373 6c00b545 2018-01-17 stsp err = got_error_from_errno();
376 6c00b545 2018-01-17 stsp err = got_packfile_open_object(obj, id, repo);
379 6c00b545 2018-01-17 stsp if (*obj == NULL)
380 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NO_OBJ);
382 2178c42e 2018-04-22 stsp err = read_object_header_privsep(obj, fd);
385 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
388 7bb0daa1 2018-06-21 stsp if (err == NULL) {
389 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
390 7bb0daa1 2018-06-21 stsp err = got_repo_cache_object(repo, id, *obj);
393 6c00b545 2018-01-17 stsp free(path);
394 2178c42e 2018-04-22 stsp if (fd != -1)
396 ab9a70b2 2017-11-06 stsp return err;
400 6dfa2fd3 2018-02-12 stsp const struct got_error *
401 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
402 6dfa2fd3 2018-02-12 stsp const char *id_str)
404 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
406 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
407 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
409 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
413 ab9a70b2 2017-11-06 stsp got_object_close(struct got_object *obj)
415 7bb0daa1 2018-06-21 stsp if (obj->refcnt > 0) {
416 7bb0daa1 2018-06-21 stsp obj->refcnt--;
417 cc538cdd 2018-06-22 stsp if (obj->refcnt > 0)
421 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
422 c3703302 2018-01-23 stsp struct got_delta *delta;
423 96f5e8b3 2018-01-23 stsp while (!SIMPLEQ_EMPTY(&obj->deltas.entries)) {
424 c3703302 2018-01-23 stsp delta = SIMPLEQ_FIRST(&obj->deltas.entries);
425 96f5e8b3 2018-01-23 stsp SIMPLEQ_REMOVE_HEAD(&obj->deltas.entries, entry);
426 c3703302 2018-01-23 stsp got_delta_close(delta);
429 96f5e8b3 2018-01-23 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
430 96f5e8b3 2018-01-23 stsp free(obj->path_packfile);
434 bff6ca00 2018-04-23 stsp struct got_commit_object *
435 bff6ca00 2018-04-23 stsp got_object_commit_alloc_partial(void)
437 bff6ca00 2018-04-23 stsp struct got_commit_object *commit;
439 bff6ca00 2018-04-23 stsp commit = calloc(1, sizeof(*commit));
440 bff6ca00 2018-04-23 stsp if (commit == NULL)
441 bff6ca00 2018-04-23 stsp return NULL;
442 bff6ca00 2018-04-23 stsp commit->tree_id = calloc(1, sizeof(*commit->tree_id));
443 bff6ca00 2018-04-23 stsp if (commit->tree_id == NULL) {
444 bff6ca00 2018-04-23 stsp free(commit);
445 bff6ca00 2018-04-23 stsp return NULL;
448 bff6ca00 2018-04-23 stsp SIMPLEQ_INIT(&commit->parent_ids);
450 bff6ca00 2018-04-23 stsp return commit;
453 bff6ca00 2018-04-23 stsp const struct got_error *
454 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
455 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
457 be6a1b5a 2018-06-11 stsp const struct got_error *err;
458 be6a1b5a 2018-06-11 stsp struct got_object *obj;
460 835e0dbd 2018-06-21 stsp *commit = NULL;
462 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
464 be6a1b5a 2018-06-11 stsp return err;
465 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
466 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
470 be6a1b5a 2018-06-11 stsp err = got_object_commit_open(commit, repo, obj);
472 be6a1b5a 2018-06-11 stsp got_object_close(obj);
473 be6a1b5a 2018-06-11 stsp return err;
476 be6a1b5a 2018-06-11 stsp const struct got_error *
477 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
479 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
481 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
482 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
483 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
485 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
486 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
487 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
488 dbc6a6b6 2018-07-12 stsp free(*qid);
489 dbc6a6b6 2018-07-12 stsp *qid = NULL;
490 dbc6a6b6 2018-07-12 stsp return err;
493 dbc6a6b6 2018-07-12 stsp return NULL;
497 dbc6a6b6 2018-07-12 stsp got_object_qid_free(struct got_object_qid *qid)
499 dbc6a6b6 2018-07-12 stsp free(qid->id);
503 dbc6a6b6 2018-07-12 stsp const struct got_error *
504 bff6ca00 2018-04-23 stsp got_object_commit_add_parent(struct got_commit_object *commit,
505 bff6ca00 2018-04-23 stsp const char *id_str)
507 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
508 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
510 062ebb78 2018-07-12 stsp qid = malloc(sizeof(*qid));
511 79f35eb3 2018-06-11 stsp if (qid == NULL)
512 bff6ca00 2018-04-23 stsp return got_error_from_errno();
514 062ebb78 2018-07-12 stsp qid->id = malloc(sizeof(*qid->id));
515 79f35eb3 2018-06-11 stsp if (qid->id == NULL) {
516 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
518 bff6ca00 2018-04-23 stsp return err;
521 79f35eb3 2018-06-11 stsp if (!got_parse_sha1_digest(qid->id->sha1, id_str)) {
522 bff6ca00 2018-04-23 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
523 79f35eb3 2018-06-11 stsp free(qid->id);
525 bff6ca00 2018-04-23 stsp return err;
528 79f35eb3 2018-06-11 stsp SIMPLEQ_INSERT_TAIL(&commit->parent_ids, qid, entry);
529 bff6ca00 2018-04-23 stsp commit->nparents++;
531 4626e416 2018-06-10 stsp return NULL;
534 4626e416 2018-06-10 stsp static const struct got_error *
535 788c352e 2018-06-16 stsp parse_gmtoff(time_t *gmtoff, const char *tzstr)
537 788c352e 2018-06-16 stsp int sign = 1;
538 788c352e 2018-06-16 stsp const char *p = tzstr;
539 788c352e 2018-06-16 stsp time_t h, m;
541 788c352e 2018-06-16 stsp *gmtoff = 0;
543 788c352e 2018-06-16 stsp if (*p == '-')
545 788c352e 2018-06-16 stsp else if (*p != '+')
546 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
548 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
549 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
550 788c352e 2018-06-16 stsp h = (((*p - '0') * 10) + (*(p + 1) - '0'));
553 788c352e 2018-06-16 stsp if (!isdigit(*p) && !isdigit(*(p + 1)))
554 788c352e 2018-06-16 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
555 788c352e 2018-06-16 stsp m = ((*p - '0') * 10) + (*(p + 1) - '0');
557 788c352e 2018-06-16 stsp *gmtoff = (h * 60 * 60 + m * 60) * sign;
558 788c352e 2018-06-16 stsp return NULL;
561 788c352e 2018-06-16 stsp static const struct got_error *
562 788c352e 2018-06-16 stsp parse_commit_time(struct tm *tm, char *committer)
564 788c352e 2018-06-16 stsp const struct got_error *err = NULL;
565 788c352e 2018-06-16 stsp const char *errstr;
566 788c352e 2018-06-16 stsp char *space, *tzstr;
567 788c352e 2018-06-16 stsp time_t gmtoff;
568 788c352e 2018-06-16 stsp time_t time;
570 788c352e 2018-06-16 stsp /* Parse and strip off trailing timezone indicator string. */
571 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
572 4626e416 2018-06-10 stsp if (space == NULL)
573 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
574 788c352e 2018-06-16 stsp tzstr = strdup(space + 1);
575 788c352e 2018-06-16 stsp if (tzstr == NULL)
576 6c281f94 2018-06-11 stsp return got_error_from_errno();
577 788c352e 2018-06-16 stsp err = parse_gmtoff(&gmtoff, tzstr);
578 788c352e 2018-06-16 stsp free(tzstr);
580 788c352e 2018-06-16 stsp return err;
581 4626e416 2018-06-10 stsp *space = '\0';
583 4626e416 2018-06-10 stsp /* Timestamp is separated from committer name + email by space. */
584 4626e416 2018-06-10 stsp space = strrchr(committer, ' ');
585 4626e416 2018-06-10 stsp if (space == NULL)
586 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
588 788c352e 2018-06-16 stsp /* Timestamp parsed here is expressed in comitter's local time. */
589 788c352e 2018-06-16 stsp time = strtonum(space + 1, 0, INT64_MAX, &errstr);
590 4626e416 2018-06-10 stsp if (errstr)
591 4626e416 2018-06-10 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
593 788c352e 2018-06-16 stsp /* Express the time stamp in UTC. */
594 788c352e 2018-06-16 stsp memset(tm, 0, sizeof(*tm));
595 788c352e 2018-06-16 stsp time -= gmtoff;
596 788c352e 2018-06-16 stsp if (localtime_r(&time, tm) == NULL)
597 788c352e 2018-06-16 stsp return got_error_from_errno();
598 788c352e 2018-06-16 stsp tm->tm_gmtoff = gmtoff;
600 4626e416 2018-06-10 stsp /* Strip off parsed time information, leaving just author and email. */
601 4626e416 2018-06-10 stsp *space = '\0';
603 bff6ca00 2018-04-23 stsp return NULL;
606 d1cda826 2017-11-06 stsp static const struct got_error *
607 d1cda826 2017-11-06 stsp parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
609 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
610 d1cda826 2017-11-06 stsp char *s = buf;
611 d1cda826 2017-11-06 stsp size_t tlen;
612 d1cda826 2017-11-06 stsp ssize_t remain = (ssize_t)len;
614 bff6ca00 2018-04-23 stsp *commit = got_object_commit_alloc_partial();
615 d1cda826 2017-11-06 stsp if (*commit == NULL)
616 0a585a0d 2018-03-17 stsp return got_error_from_errno();
618 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_TREE);
619 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_TREE, 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 59ece79d 2018-02-12 stsp if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
627 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
630 d1cda826 2017-11-06 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
631 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
633 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
637 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_PARENT);
638 d1cda826 2017-11-06 stsp while (strncmp(s, GOT_COMMIT_TAG_PARENT, tlen) == 0) {
639 d1cda826 2017-11-06 stsp remain -= tlen;
640 d1cda826 2017-11-06 stsp if (remain < SHA1_DIGEST_STRING_LENGTH) {
641 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
645 bff6ca00 2018-04-23 stsp err = got_object_commit_add_parent(*commit, s);
649 eb651edf 2018-02-11 stsp remain -= SHA1_DIGEST_STRING_LENGTH;
650 d1cda826 2017-11-06 stsp s += SHA1_DIGEST_STRING_LENGTH;
653 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_AUTHOR);
654 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_AUTHOR, tlen) == 0) {
656 4626e416 2018-06-10 stsp size_t slen;
658 d1cda826 2017-11-06 stsp remain -= tlen;
659 d1cda826 2017-11-06 stsp if (remain <= 0) {
660 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
664 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
665 d1cda826 2017-11-06 stsp if (p == NULL) {
666 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
670 4626e416 2018-06-10 stsp slen = strlen(s);
671 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_author, s);
674 d1cda826 2017-11-06 stsp (*commit)->author = strdup(s);
675 d1cda826 2017-11-06 stsp if ((*commit)->author == NULL) {
676 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
679 4626e416 2018-06-10 stsp s += slen + 1;
680 4626e416 2018-06-10 stsp remain -= slen + 1;
683 d1cda826 2017-11-06 stsp tlen = strlen(GOT_COMMIT_TAG_COMMITTER);
684 d1cda826 2017-11-06 stsp if (strncmp(s, GOT_COMMIT_TAG_COMMITTER, tlen) == 0) {
686 4626e416 2018-06-10 stsp size_t slen;
688 d1cda826 2017-11-06 stsp remain -= tlen;
689 d1cda826 2017-11-06 stsp if (remain <= 0) {
690 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
694 d1cda826 2017-11-06 stsp p = strchr(s, '\n');
695 d1cda826 2017-11-06 stsp if (p == NULL) {
696 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
700 4626e416 2018-06-10 stsp slen = strlen(s);
701 788c352e 2018-06-16 stsp err = parse_commit_time(&(*commit)->tm_committer, s);
704 d1cda826 2017-11-06 stsp (*commit)->committer = strdup(s);
705 d1cda826 2017-11-06 stsp if ((*commit)->committer == NULL) {
706 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
709 4626e416 2018-06-10 stsp s += slen + 1;
710 4626e416 2018-06-10 stsp remain -= slen + 1;
713 eb651edf 2018-02-11 stsp (*commit)->logmsg = strndup(s, remain);
714 eb651edf 2018-02-11 stsp if ((*commit)->logmsg == NULL) {
715 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
720 d1cda826 2017-11-06 stsp got_object_commit_close(*commit);
721 59ece79d 2018-02-12 stsp *commit = NULL;
723 0ffeb3c2 2017-11-26 stsp return err;
726 0ffeb3c2 2017-11-26 stsp static void
727 0ffeb3c2 2017-11-26 stsp tree_entry_close(struct got_tree_entry *te)
729 59ece79d 2018-02-12 stsp free(te->id);
730 0ffeb3c2 2017-11-26 stsp free(te->name);
734 e033d803 2018-04-23 stsp struct got_tree_entry *
735 e033d803 2018-04-23 stsp got_alloc_tree_entry_partial(void)
737 e033d803 2018-04-23 stsp struct got_tree_entry *te;
739 e033d803 2018-04-23 stsp te = calloc(1, sizeof(*te));
740 e033d803 2018-04-23 stsp if (te == NULL)
741 e033d803 2018-04-23 stsp return NULL;
743 e033d803 2018-04-23 stsp te->id = calloc(1, sizeof(*te->id));
744 e033d803 2018-04-23 stsp if (te->id == NULL) {
751 0ffeb3c2 2017-11-26 stsp static const struct got_error *
752 0ffeb3c2 2017-11-26 stsp parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
753 0ffeb3c2 2017-11-26 stsp size_t maxlen)
755 0ffeb3c2 2017-11-26 stsp char *p = buf, *space;
756 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
758 e033d803 2018-04-23 stsp *te = got_alloc_tree_entry_partial();
759 0ffeb3c2 2017-11-26 stsp if (*te == NULL)
760 0a585a0d 2018-03-17 stsp return got_error_from_errno();
762 0ffeb3c2 2017-11-26 stsp *elen = strlen(buf) + 1;
763 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen) {
765 59ece79d 2018-02-12 stsp *te = NULL;
766 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
769 0ffeb3c2 2017-11-26 stsp space = strchr(buf, ' ');
770 0ffeb3c2 2017-11-26 stsp if (space == NULL) {
771 0a585a0d 2018-03-17 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
773 59ece79d 2018-02-12 stsp *te = NULL;
774 0a585a0d 2018-03-17 stsp return err;
776 0ffeb3c2 2017-11-26 stsp while (*p != ' ') {
777 0ffeb3c2 2017-11-26 stsp if (*p < '0' && *p > '7') {
778 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
781 0ffeb3c2 2017-11-26 stsp (*te)->mode <<= 3;
782 0ffeb3c2 2017-11-26 stsp (*te)->mode |= *p - '0';
786 0ffeb3c2 2017-11-26 stsp (*te)->name = strdup(space + 1);
787 0ffeb3c2 2017-11-26 stsp if (*elen > maxlen || maxlen - *elen < SHA1_DIGEST_LENGTH) {
788 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
791 0ffeb3c2 2017-11-26 stsp buf += strlen(buf) + 1;
792 59ece79d 2018-02-12 stsp memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
793 0ffeb3c2 2017-11-26 stsp *elen += SHA1_DIGEST_LENGTH;
796 0ffeb3c2 2017-11-26 stsp tree_entry_close(*te);
797 59ece79d 2018-02-12 stsp *te = NULL;
799 d1cda826 2017-11-06 stsp return err;
802 d1cda826 2017-11-06 stsp static const struct got_error *
803 e033d803 2018-04-23 stsp parse_tree_object(struct got_tree_object **tree, uint8_t *buf, size_t len)
805 90356acc 2018-01-27 stsp const struct got_error *err;
806 0ffeb3c2 2017-11-26 stsp size_t remain = len;
808 0ffeb3c2 2017-11-26 stsp *tree = calloc(1, sizeof(**tree));
809 0ffeb3c2 2017-11-26 stsp if (*tree == NULL)
810 0a585a0d 2018-03-17 stsp return got_error_from_errno();
812 883f0469 2018-06-23 stsp SIMPLEQ_INIT(&(*tree)->entries.head);
814 0ffeb3c2 2017-11-26 stsp while (remain > 0) {
815 0ffeb3c2 2017-11-26 stsp struct got_tree_entry *te;
816 0ffeb3c2 2017-11-26 stsp size_t elen;
818 90356acc 2018-01-27 stsp err = parse_tree_entry(&te, &elen, buf, remain);
820 90356acc 2018-01-27 stsp return err;
821 883f0469 2018-06-23 stsp (*tree)->entries.nentries++;
822 883f0469 2018-06-23 stsp SIMPLEQ_INSERT_TAIL(&(*tree)->entries.head, te, entry);
823 0ffeb3c2 2017-11-26 stsp buf += elen;
824 0ffeb3c2 2017-11-26 stsp remain -= elen;
827 0ffeb3c2 2017-11-26 stsp if (remain != 0) {
828 0ffeb3c2 2017-11-26 stsp got_object_tree_close(*tree);
829 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_BAD_OBJ_DATA);
832 0ffeb3c2 2017-11-26 stsp return NULL;
835 0ffeb3c2 2017-11-26 stsp static const struct got_error *
836 eb651edf 2018-02-11 stsp read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
838 eb651edf 2018-02-11 stsp const struct got_error *err = NULL;
839 eb651edf 2018-02-11 stsp static const size_t blocksize = 512;
840 eb651edf 2018-02-11 stsp size_t n, total, remain;
841 eb651edf 2018-02-11 stsp uint8_t *buf;
843 eb651edf 2018-02-11 stsp *outbuf = NULL;
844 eb651edf 2018-02-11 stsp *outlen = 0;
846 062ebb78 2018-07-12 stsp buf = malloc(blocksize);
847 eb651edf 2018-02-11 stsp if (buf == NULL)
848 0a585a0d 2018-03-17 stsp return got_error_from_errno();
850 eb651edf 2018-02-11 stsp remain = blocksize;
852 eb651edf 2018-02-11 stsp while (1) {
853 eb651edf 2018-02-11 stsp if (remain == 0) {
854 eb651edf 2018-02-11 stsp uint8_t *newbuf;
855 eb651edf 2018-02-11 stsp newbuf = reallocarray(buf, 1, total + blocksize);
856 eb651edf 2018-02-11 stsp if (newbuf == NULL) {
857 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
860 eb651edf 2018-02-11 stsp buf = newbuf;
861 eb651edf 2018-02-11 stsp remain += blocksize;
863 be89e2b1 2018-03-03 stsp n = fread(buf + total, 1, remain, f);
864 eb651edf 2018-02-11 stsp if (n == 0) {
865 eb651edf 2018-02-11 stsp if (ferror(f)) {
866 eb651edf 2018-02-11 stsp err = got_ferror(f, GOT_ERR_IO);
869 eb651edf 2018-02-11 stsp break; /* EOF */
871 eb651edf 2018-02-11 stsp remain -= n;
872 eb651edf 2018-02-11 stsp total += n;
876 eb651edf 2018-02-11 stsp if (err == NULL) {
877 eb651edf 2018-02-11 stsp *outbuf = buf;
878 eb651edf 2018-02-11 stsp *outlen = total;
881 eb651edf 2018-02-11 stsp return err;
884 eb651edf 2018-02-11 stsp static const struct got_error *
885 bff6ca00 2018-04-23 stsp read_commit_object(struct got_commit_object **commit, struct got_object *obj,
888 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
889 d1cda826 2017-11-06 stsp size_t len;
890 eb651edf 2018-02-11 stsp uint8_t *p;
892 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
893 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
895 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
897 d1cda826 2017-11-06 stsp return err;
899 d1cda826 2017-11-06 stsp if (len < obj->hdrlen + obj->size) {
900 d1cda826 2017-11-06 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
904 d1cda826 2017-11-06 stsp /* Skip object header. */
905 d1cda826 2017-11-06 stsp len -= obj->hdrlen;
906 eb651edf 2018-02-11 stsp err = parse_commit_object(commit, p + obj->hdrlen, len);
909 d1cda826 2017-11-06 stsp return err;
912 bff6ca00 2018-04-23 stsp static void
913 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(struct got_object *obj, int obj_fd,
914 bff6ca00 2018-04-23 stsp int imsg_fds[2])
916 bff6ca00 2018-04-23 stsp const struct got_error *err = NULL;
917 bff6ca00 2018-04-23 stsp struct got_commit_object *commit = NULL;
918 bff6ca00 2018-04-23 stsp struct imsgbuf ibuf;
919 bff6ca00 2018-04-23 stsp FILE *f = NULL;
920 bff6ca00 2018-04-23 stsp int status = 0;
922 be37c2e6 2018-04-24 stsp setproctitle("read commit object");
923 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
924 bff6ca00 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
926 bff6ca00 2018-04-23 stsp /* revoke access to most system calls */
927 bff6ca00 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
928 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
932 bff6ca00 2018-04-23 stsp f = fdopen(obj_fd, "rb");
933 bff6ca00 2018-04-23 stsp if (f == NULL) {
934 bff6ca00 2018-04-23 stsp err = got_error_from_errno();
935 bff6ca00 2018-04-23 stsp close(obj_fd);
939 bff6ca00 2018-04-23 stsp err = read_commit_object(&commit, obj, f);
943 068fd2bf 2018-04-24 stsp err = got_privsep_send_commit(&ibuf, commit);
945 bff6ca00 2018-04-23 stsp if (commit)
946 bff6ca00 2018-04-23 stsp got_object_commit_close(commit);
948 bff6ca00 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
949 bff6ca00 2018-04-23 stsp status = 1;
953 bff6ca00 2018-04-23 stsp imsg_clear(&ibuf);
954 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
955 bff6ca00 2018-04-23 stsp _exit(status);
958 bff6ca00 2018-04-23 stsp static const struct got_error *
959 bff6ca00 2018-04-23 stsp read_commit_object_privsep(struct got_commit_object **commit,
960 bff6ca00 2018-04-23 stsp struct got_repository *repo, struct got_object *obj, int fd)
962 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
963 bff6ca00 2018-04-23 stsp struct imsgbuf parent_ibuf;
964 bff6ca00 2018-04-23 stsp int imsg_fds[2];
967 bff6ca00 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
968 bff6ca00 2018-04-23 stsp return got_error_from_errno();
970 bff6ca00 2018-04-23 stsp pid = fork();
971 bff6ca00 2018-04-23 stsp if (pid == -1)
972 bff6ca00 2018-04-23 stsp return got_error_from_errno();
973 bff6ca00 2018-04-23 stsp else if (pid == 0) {
974 bff6ca00 2018-04-23 stsp read_commit_object_privsep_child(obj, fd, imsg_fds);
975 e506bf32 2018-04-23 stsp /* not reached */
978 bff6ca00 2018-04-23 stsp close(imsg_fds[1]);
979 bff6ca00 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
980 068fd2bf 2018-04-24 stsp err = got_privsep_recv_commit(commit, &parent_ibuf);
981 bff6ca00 2018-04-23 stsp imsg_clear(&parent_ibuf);
982 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
983 bff6ca00 2018-04-23 stsp close(imsg_fds[0]);
984 b419fc47 2018-04-26 stsp return err ? err : err_child;
987 d1cda826 2017-11-06 stsp const struct got_error *
988 d1cda826 2017-11-06 stsp got_object_commit_open(struct got_commit_object **commit,
989 d1cda826 2017-11-06 stsp struct got_repository *repo, struct got_object *obj)
991 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
993 1943de01 2018-06-22 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
994 1943de01 2018-06-22 stsp if (*commit != NULL) {
995 1943de01 2018-06-22 stsp (*commit)->refcnt++;
996 1943de01 2018-06-22 stsp return NULL;
999 d1cda826 2017-11-06 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
1000 d1cda826 2017-11-06 stsp return got_error(GOT_ERR_OBJ_TYPE);
1002 ea35256b 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1003 ea35256b 2018-03-16 stsp uint8_t *buf;
1004 ea35256b 2018-03-16 stsp size_t len;
1005 ea35256b 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1007 ea35256b 2018-03-16 stsp return err;
1008 b29656e2 2018-03-16 stsp obj->size = len;
1009 b29656e2 2018-03-16 stsp err = parse_commit_object(commit, buf, len);
1010 ea35256b 2018-03-16 stsp free(buf);
1013 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1015 ea35256b 2018-03-16 stsp return err;
1016 bff6ca00 2018-04-23 stsp err = read_commit_object_privsep(commit, repo, obj, fd);
1017 bff6ca00 2018-04-23 stsp close(fd);
1020 1943de01 2018-06-22 stsp if (err == NULL) {
1021 1943de01 2018-06-22 stsp (*commit)->refcnt++;
1022 1943de01 2018-06-22 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
1025 d1cda826 2017-11-06 stsp return err;
1029 d1cda826 2017-11-06 stsp got_object_commit_close(struct got_commit_object *commit)
1031 79f35eb3 2018-06-11 stsp struct got_object_qid *qid;
1033 1943de01 2018-06-22 stsp if (commit->refcnt > 0) {
1034 1943de01 2018-06-22 stsp commit->refcnt--;
1035 cc538cdd 2018-06-22 stsp if (commit->refcnt > 0)
1039 d1cda826 2017-11-06 stsp while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
1040 79f35eb3 2018-06-11 stsp qid = SIMPLEQ_FIRST(&commit->parent_ids);
1041 d1cda826 2017-11-06 stsp SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
1042 79f35eb3 2018-06-11 stsp free(qid->id);
1043 79f35eb3 2018-06-11 stsp free(qid);
1046 59ece79d 2018-02-12 stsp free(commit->tree_id);
1047 d1cda826 2017-11-06 stsp free(commit->author);
1048 d1cda826 2017-11-06 stsp free(commit->committer);
1049 d1cda826 2017-11-06 stsp free(commit->logmsg);
1050 d1cda826 2017-11-06 stsp free(commit);
1053 0ffeb3c2 2017-11-26 stsp static const struct got_error *
1054 e033d803 2018-04-23 stsp read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
1056 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1057 0ffeb3c2 2017-11-26 stsp size_t len;
1058 eb651edf 2018-02-11 stsp uint8_t *p;
1060 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED)
1061 eb651edf 2018-02-11 stsp err = read_to_mem(&p, &len, f);
1063 eb651edf 2018-02-11 stsp err = got_inflate_to_mem(&p, &len, f);
1065 0ffeb3c2 2017-11-26 stsp return err;
1067 0ffeb3c2 2017-11-26 stsp if (len < obj->hdrlen + obj->size) {
1068 0ffeb3c2 2017-11-26 stsp err = got_error(GOT_ERR_BAD_OBJ_DATA);
1069 0ffeb3c2 2017-11-26 stsp goto done;
1072 0ffeb3c2 2017-11-26 stsp /* Skip object header. */
1073 0ffeb3c2 2017-11-26 stsp len -= obj->hdrlen;
1074 e033d803 2018-04-23 stsp err = parse_tree_object(tree, p + obj->hdrlen, len);
1077 e033d803 2018-04-23 stsp return err;
1080 e033d803 2018-04-23 stsp static void
1081 e033d803 2018-04-23 stsp read_tree_object_privsep_child(struct got_object *obj, int obj_fd,
1082 e033d803 2018-04-23 stsp int imsg_fds[2])
1084 e033d803 2018-04-23 stsp const struct got_error *err = NULL;
1085 e033d803 2018-04-23 stsp struct got_tree_object *tree = NULL;
1086 e033d803 2018-04-23 stsp struct imsgbuf ibuf;
1087 e033d803 2018-04-23 stsp FILE *f = NULL;
1088 e033d803 2018-04-23 stsp int status = 0;
1090 be37c2e6 2018-04-24 stsp setproctitle("read tree object");
1091 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1092 e033d803 2018-04-23 stsp imsg_init(&ibuf, imsg_fds[1]);
1094 e033d803 2018-04-23 stsp /* revoke access to most system calls */
1095 e033d803 2018-04-23 stsp if (pledge("stdio", NULL) == -1) {
1096 e033d803 2018-04-23 stsp err = got_error_from_errno();
1097 e033d803 2018-04-23 stsp goto done;
1100 e033d803 2018-04-23 stsp f = fdopen(obj_fd, "rb");
1101 e033d803 2018-04-23 stsp if (f == NULL) {
1102 e033d803 2018-04-23 stsp err = got_error_from_errno();
1103 e033d803 2018-04-23 stsp close(obj_fd);
1104 e033d803 2018-04-23 stsp goto done;
1107 e033d803 2018-04-23 stsp err = read_tree_object(&tree, obj, f);
1109 e033d803 2018-04-23 stsp goto done;
1111 068fd2bf 2018-04-24 stsp err = got_privsep_send_tree(&ibuf, tree);
1114 e033d803 2018-04-23 stsp got_object_tree_close(tree);
1115 e033d803 2018-04-23 stsp if (err) {
1116 e033d803 2018-04-23 stsp got_privsep_send_error(&ibuf, err);
1117 e033d803 2018-04-23 stsp status = 1;
1120 e033d803 2018-04-23 stsp fclose(f);
1121 e033d803 2018-04-23 stsp imsg_clear(&ibuf);
1122 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1123 e033d803 2018-04-23 stsp _exit(status);
1126 e033d803 2018-04-23 stsp static const struct got_error *
1127 e033d803 2018-04-23 stsp read_tree_object_privsep(struct got_tree_object **tree, struct got_object *obj,
1130 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1131 e033d803 2018-04-23 stsp struct imsgbuf parent_ibuf;
1132 e033d803 2018-04-23 stsp int imsg_fds[2];
1133 e033d803 2018-04-23 stsp pid_t pid;
1135 e033d803 2018-04-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1136 e033d803 2018-04-23 stsp return got_error_from_errno();
1138 e033d803 2018-04-23 stsp pid = fork();
1139 e033d803 2018-04-23 stsp if (pid == -1)
1140 e033d803 2018-04-23 stsp return got_error_from_errno();
1141 e033d803 2018-04-23 stsp else if (pid == 0) {
1142 e033d803 2018-04-23 stsp read_tree_object_privsep_child(obj, fd, imsg_fds);
1143 e033d803 2018-04-23 stsp /* not reached */
1146 e033d803 2018-04-23 stsp close(imsg_fds[1]);
1147 e033d803 2018-04-23 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1148 068fd2bf 2018-04-24 stsp err = got_privsep_recv_tree(tree, &parent_ibuf);
1149 e033d803 2018-04-23 stsp imsg_clear(&parent_ibuf);
1150 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1151 e033d803 2018-04-23 stsp close(imsg_fds[0]);
1152 b419fc47 2018-04-26 stsp return err ? err : err_child;
1155 0ffeb3c2 2017-11-26 stsp const struct got_error *
1156 0ffeb3c2 2017-11-26 stsp got_object_tree_open(struct got_tree_object **tree,
1157 0ffeb3c2 2017-11-26 stsp struct got_repository *repo, struct got_object *obj)
1159 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1161 f6be5c30 2018-06-22 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
1162 f6be5c30 2018-06-22 stsp if (*tree != NULL) {
1163 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1164 f6be5c30 2018-06-22 stsp return NULL;
1167 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
1168 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
1170 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1171 e0ab43e7 2018-03-16 stsp uint8_t *buf;
1172 e0ab43e7 2018-03-16 stsp size_t len;
1173 e0ab43e7 2018-03-16 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, repo);
1175 e0ab43e7 2018-03-16 stsp return err;
1176 b29656e2 2018-03-16 stsp obj->size = len;
1177 e033d803 2018-04-23 stsp err = parse_tree_object(tree, buf, len);
1178 e0ab43e7 2018-03-16 stsp free(buf);
1181 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
1183 e0ab43e7 2018-03-16 stsp return err;
1184 e033d803 2018-04-23 stsp err = read_tree_object_privsep(tree, obj, fd);
1185 d5003b79 2018-04-22 stsp close(fd);
1188 f6be5c30 2018-06-22 stsp if (err == NULL) {
1189 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1190 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
1193 776d4d29 2018-06-17 stsp return err;
1196 776d4d29 2018-06-17 stsp const struct got_error *
1197 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1198 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1200 776d4d29 2018-06-17 stsp const struct got_error *err;
1201 776d4d29 2018-06-17 stsp struct got_object *obj;
1203 835e0dbd 2018-06-21 stsp *tree = NULL;
1205 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
1207 776d4d29 2018-06-17 stsp return err;
1208 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
1209 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1210 776d4d29 2018-06-17 stsp goto done;
1213 776d4d29 2018-06-17 stsp err = got_object_tree_open(tree, repo, obj);
1215 776d4d29 2018-06-17 stsp got_object_close(obj);
1216 0ffeb3c2 2017-11-26 stsp return err;
1220 0ffeb3c2 2017-11-26 stsp got_object_tree_close(struct got_tree_object *tree)
1222 f715ca7f 2017-11-27 stsp struct got_tree_entry *te;
1224 f6be5c30 2018-06-22 stsp if (tree->refcnt > 0) {
1225 f6be5c30 2018-06-22 stsp tree->refcnt--;
1226 cc538cdd 2018-06-22 stsp if (tree->refcnt > 0)
1230 883f0469 2018-06-23 stsp while (!SIMPLEQ_EMPTY(&tree->entries.head)) {
1231 883f0469 2018-06-23 stsp te = SIMPLEQ_FIRST(&tree->entries.head);
1232 883f0469 2018-06-23 stsp SIMPLEQ_REMOVE_HEAD(&tree->entries.head, entry);
1233 f715ca7f 2017-11-27 stsp tree_entry_close(te);
1236 f715ca7f 2017-11-27 stsp free(tree);
1239 883f0469 2018-06-23 stsp const struct got_tree_entries *
1240 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
1242 883f0469 2018-06-23 stsp return &tree->entries;
1245 ff6b18f8 2018-04-24 stsp static const struct got_error *
1246 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(int outfd, int infd, int imsg_fds[2])
1248 ff6b18f8 2018-04-24 stsp const struct got_error *err = NULL;
1249 ff6b18f8 2018-04-24 stsp struct imsgbuf ibuf;
1250 ff6b18f8 2018-04-24 stsp int status = 0;
1251 2967a784 2018-04-24 stsp size_t size;
1252 8b2180d4 2018-04-26 stsp FILE *infile = NULL;
1254 be37c2e6 2018-04-24 stsp setproctitle("read blob object");
1255 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1256 ff6b18f8 2018-04-24 stsp imsg_init(&ibuf, imsg_fds[1]);
1258 ff6b18f8 2018-04-24 stsp /* revoke access to most system calls */
1259 ff6b18f8 2018-04-24 stsp if (pledge("stdio", NULL) == -1) {
1260 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1261 ff6b18f8 2018-04-24 stsp goto done;
1264 8b2180d4 2018-04-26 stsp infile = fdopen(infd, "rb");
1265 8b2180d4 2018-04-26 stsp if (infile == NULL) {
1266 8b2180d4 2018-04-26 stsp err = got_error_from_errno();
1267 8b2180d4 2018-04-26 stsp close(infd);
1268 8b2180d4 2018-04-26 stsp goto done;
1270 8b2180d4 2018-04-26 stsp err = got_inflate_to_fd(&size, infile, outfd);
1271 8b2180d4 2018-04-26 stsp fclose(infile);
1273 ff6b18f8 2018-04-24 stsp goto done;
1275 2967a784 2018-04-24 stsp err = got_privsep_send_blob(&ibuf, size);
1277 ff6b18f8 2018-04-24 stsp if (err) {
1278 ff6b18f8 2018-04-24 stsp got_privsep_send_error(&ibuf, err);
1279 ff6b18f8 2018-04-24 stsp status = 1;
1281 ff6b18f8 2018-04-24 stsp close(outfd);
1282 ff6b18f8 2018-04-24 stsp imsg_clear(&ibuf);
1283 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1284 ff6b18f8 2018-04-24 stsp _exit(status);
1287 ff6b18f8 2018-04-24 stsp static const struct got_error *
1288 2967a784 2018-04-24 stsp read_blob_object_privsep(size_t *size, int outfd, int infd)
1290 ff6b18f8 2018-04-24 stsp struct imsgbuf parent_ibuf;
1291 ff6b18f8 2018-04-24 stsp int imsg_fds[2];
1292 b419fc47 2018-04-26 stsp const struct got_error *err = NULL, *err_child = NULL;
1293 ff6b18f8 2018-04-24 stsp pid_t pid;
1295 ff6b18f8 2018-04-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1296 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1298 ff6b18f8 2018-04-24 stsp pid = fork();
1299 ff6b18f8 2018-04-24 stsp if (pid == -1)
1300 ff6b18f8 2018-04-24 stsp return got_error_from_errno();
1301 ff6b18f8 2018-04-24 stsp else if (pid == 0) {
1302 ff6b18f8 2018-04-24 stsp read_blob_object_privsep_child(outfd, infd, imsg_fds);
1303 ff6b18f8 2018-04-24 stsp /* not reached */
1306 ff6b18f8 2018-04-24 stsp close(imsg_fds[1]);
1307 ff6b18f8 2018-04-24 stsp imsg_init(&parent_ibuf, imsg_fds[0]);
1308 2967a784 2018-04-24 stsp err = got_privsep_recv_blob(size, &parent_ibuf);
1309 ff6b18f8 2018-04-24 stsp imsg_clear(&parent_ibuf);
1310 b419fc47 2018-04-26 stsp err_child = wait_for_child(pid);
1311 ff6b18f8 2018-04-24 stsp close(imsg_fds[0]);
1312 ff6b18f8 2018-04-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1313 ff6b18f8 2018-04-24 stsp err = got_error_from_errno();
1314 b419fc47 2018-04-26 stsp return err ? err : err_child;
1317 68482ea3 2017-11-27 stsp const struct got_error *
1318 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
1319 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1321 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1323 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
1324 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
1326 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
1327 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
1329 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1330 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1331 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1333 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1334 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1335 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
1336 c7254d79 2018-04-24 stsp goto done;
1338 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
1339 eb651edf 2018-02-11 stsp err = got_packfile_extract_object(&((*blob)->f), obj, repo);
1341 c7254d79 2018-04-24 stsp goto done;
1343 3aca5731 2018-04-24 stsp int infd, outfd;
1344 2967a784 2018-04-24 stsp size_t size;
1345 2967a784 2018-04-24 stsp struct stat sb;
1347 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
1349 c7254d79 2018-04-24 stsp goto done;
1352 3aca5731 2018-04-24 stsp outfd = got_opentempfd();
1353 3aca5731 2018-04-24 stsp if (outfd == -1) {
1354 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1355 3aca5731 2018-04-24 stsp close(infd);
1356 3aca5731 2018-04-24 stsp goto done;
1359 2967a784 2018-04-24 stsp err = read_blob_object_privsep(&size, outfd, infd);
1360 3aca5731 2018-04-24 stsp close(infd);
1362 c7254d79 2018-04-24 stsp goto done;
1364 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
1365 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1366 2967a784 2018-04-24 stsp close(outfd);
1367 2967a784 2018-04-24 stsp goto done;
1370 2967a784 2018-04-24 stsp if (fstat(outfd, &sb) == -1) {
1371 2967a784 2018-04-24 stsp err = got_error_from_errno();
1372 2967a784 2018-04-24 stsp close(outfd);
1373 2967a784 2018-04-24 stsp goto done;
1376 2967a784 2018-04-24 stsp if (sb.st_size != size) {
1377 2967a784 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1378 2967a784 2018-04-24 stsp close(outfd);
1379 2967a784 2018-04-24 stsp goto done;
1382 3aca5731 2018-04-24 stsp (*blob)->f = fdopen(outfd, "rb");
1383 3aca5731 2018-04-24 stsp if ((*blob)->f == NULL) {
1384 3aca5731 2018-04-24 stsp err = got_error_from_errno();
1385 3aca5731 2018-04-24 stsp close(outfd);
1386 3aca5731 2018-04-24 stsp goto done;
1390 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
1391 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1392 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
1395 c7254d79 2018-04-24 stsp if (err && *blob) {
1396 c7254d79 2018-04-24 stsp if ((*blob)->f)
1397 c7254d79 2018-04-24 stsp fclose((*blob)->f);
1398 c7254d79 2018-04-24 stsp free((*blob)->read_buf);
1399 c7254d79 2018-04-24 stsp free(*blob);
1400 c7254d79 2018-04-24 stsp *blob = NULL;
1402 a19581a2 2018-06-21 stsp return err;
1405 a19581a2 2018-06-21 stsp const struct got_error *
1406 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1407 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1408 a19581a2 2018-06-21 stsp size_t blocksize)
1410 a19581a2 2018-06-21 stsp const struct got_error *err;
1411 a19581a2 2018-06-21 stsp struct got_object *obj;
1413 835e0dbd 2018-06-21 stsp *blob = NULL;
1415 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
1417 a19581a2 2018-06-21 stsp return err;
1418 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
1419 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1420 a19581a2 2018-06-21 stsp goto done;
1423 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
1425 a19581a2 2018-06-21 stsp got_object_close(obj);
1426 68482ea3 2017-11-27 stsp return err;
1430 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1432 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1433 68482ea3 2017-11-27 stsp fclose(blob->f);
1434 68482ea3 2017-11-27 stsp free(blob);
1438 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1440 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1444 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1446 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1449 f934cf2c 2018-02-12 stsp const uint8_t *
1450 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1452 f934cf2c 2018-02-12 stsp return blob->read_buf;
1455 68482ea3 2017-11-27 stsp const struct got_error *
1456 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1460 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1461 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1462 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1463 eb651edf 2018-02-11 stsp *outlenp = n;
1464 35e9ba5d 2018-06-21 stsp return NULL;
1467 35e9ba5d 2018-06-21 stsp const struct got_error *
1468 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
1469 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
1471 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1472 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
1473 84451b3e 2018-07-10 stsp const uint8_t *buf;
1476 84451b3e 2018-07-10 stsp if (total_len)
1477 84451b3e 2018-07-10 stsp *total_len = 0;
1478 84451b3e 2018-07-10 stsp if (nlines)
1479 84451b3e 2018-07-10 stsp *nlines = 0;
1481 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1483 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1485 35e9ba5d 2018-06-21 stsp return err;
1486 35e9ba5d 2018-06-21 stsp if (len == 0)
1488 84451b3e 2018-07-10 stsp if (total_len)
1489 84451b3e 2018-07-10 stsp *total_len += len;
1490 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1491 84451b3e 2018-07-10 stsp if (nlines) {
1492 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
1493 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
1494 84451b3e 2018-07-10 stsp (*nlines)++;
1497 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1498 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
1499 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1500 35e9ba5d 2018-06-21 stsp } while (len != 0);
1502 35e9ba5d 2018-06-21 stsp fflush(outfile);
1503 35e9ba5d 2018-06-21 stsp rewind(outfile);
1505 776d4d29 2018-06-17 stsp return NULL;
1508 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1509 776d4d29 2018-06-17 stsp find_entry_by_name(struct got_tree_object *tree, const char *name)
1511 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1513 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1514 776d4d29 2018-06-17 stsp if (strcmp(te->name, name) == 0)
1515 776d4d29 2018-06-17 stsp return te;
1517 eb651edf 2018-02-11 stsp return NULL;
1520 776d4d29 2018-06-17 stsp const struct got_error *
1521 776d4d29 2018-06-17 stsp got_object_open_by_path(struct got_object **obj, struct got_repository *repo,
1522 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1524 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1525 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1526 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1527 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1528 197aa481 2018-06-21 stsp char *seg, *s, *s0 = NULL;
1529 67606321 2018-06-21 stsp size_t len = strlen(path);
1531 776d4d29 2018-06-17 stsp *obj = NULL;
1533 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1534 776d4d29 2018-06-17 stsp if (path[0] != '/')
1535 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1537 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1539 776d4d29 2018-06-17 stsp goto done;
1541 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1542 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1543 776d4d29 2018-06-17 stsp err = got_object_open(obj, repo, commit->tree_id);
1544 ca008b32 2018-07-22 stsp goto done;
1547 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1549 776d4d29 2018-06-17 stsp goto done;
1551 197aa481 2018-06-21 stsp s0 = strdup(path);
1552 197aa481 2018-06-21 stsp if (s0 == NULL) {
1553 776d4d29 2018-06-17 stsp err = got_error_from_errno();
1554 776d4d29 2018-06-17 stsp goto done;
1556 67606321 2018-06-21 stsp err = got_canonpath(path, s0, len + 1);
1558 776d4d29 2018-06-17 stsp goto done;
1561 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1564 67606321 2018-06-21 stsp while (len > 0) {
1565 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1567 776d4d29 2018-06-17 stsp if (*s != '/') {
1574 776d4d29 2018-06-17 stsp /* end of path segment */
1575 776d4d29 2018-06-17 stsp *s = '\0';
1577 db37e2c0 2018-06-21 stsp te = find_entry_by_name(tree, seg);
1578 db37e2c0 2018-06-21 stsp if (te == NULL) {
1579 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
1580 776d4d29 2018-06-17 stsp goto done;
1583 67606321 2018-06-21 stsp if (len == 0)
1586 776d4d29 2018-06-17 stsp seg = s + 1;
1590 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1592 db37e2c0 2018-06-21 stsp te = NULL;
1594 776d4d29 2018-06-17 stsp goto done;
1595 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1596 776d4d29 2018-06-17 stsp tree = next_tree;
1601 db37e2c0 2018-06-21 stsp err = got_object_open(obj, repo, te->id);
1603 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
1606 776d4d29 2018-06-17 stsp if (commit)
1607 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1609 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1610 776d4d29 2018-06-17 stsp return err;