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>
23 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 d71d75ad 2017-11-05 stsp #include <sha1.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 2178c42e 2018-04-22 stsp #include <imsg.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 ab9a70b2 2017-11-06 stsp #include "got_error.h"
39 d71d75ad 2017-11-05 stsp #include "got_object.h"
40 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 8bf5b3c9 2018-03-17 stsp struct got_object_id *
60 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
62 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
64 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
65 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
66 8bf5b3c9 2018-03-17 stsp return NULL;
67 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
71 3235492e 2018-04-01 stsp struct got_object_id *
72 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
74 6402fb3c 2018-09-15 stsp return &obj->id;
77 bacc9935 2018-05-20 stsp const struct got_error *
78 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
80 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
84 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
86 b107e67f 2018-01-19 stsp switch (obj->type) {
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
88 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
89 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
90 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
91 b107e67f 2018-01-19 stsp return obj->type;
97 96f5e8b3 2018-01-23 stsp /* not reached */
101 ab9a70b2 2017-11-06 stsp static const struct got_error *
102 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
104 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
105 7a132809 2018-07-23 stsp char *hex = NULL;
106 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
108 e6b1056e 2018-04-22 stsp *path = NULL;
110 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
111 0a585a0d 2018-03-17 stsp return got_error_from_errno();
113 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
117 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
118 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
119 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
123 d1cda826 2017-11-06 stsp free(path_objects);
124 d1cda826 2017-11-06 stsp return err;
127 4ee4114f 2018-01-23 stsp static const struct got_error *
128 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
130 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
131 a1fd68d8 2018-01-12 stsp char *path;
133 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
135 d1cda826 2017-11-06 stsp return err;
136 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
137 d5003b79 2018-04-22 stsp if (*fd == -1) {
138 6c00b545 2018-01-17 stsp err = got_error_from_errno();
142 4558fcd4 2018-01-14 stsp free(path);
143 2090a03d 2018-09-09 stsp return err;
146 2090a03d 2018-09-09 stsp static const struct got_error *
147 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
149 2090a03d 2018-09-09 stsp size_t size;
151 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
152 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
153 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
154 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
156 08451938 2018-11-05 stsp *path_packfile = malloc(size);
157 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
158 2090a03d 2018-09-09 stsp return got_error_from_errno();
160 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
161 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
162 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
163 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
165 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
166 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
168 2090a03d 2018-09-09 stsp return NULL;
171 2090a03d 2018-09-09 stsp static const struct got_error *
172 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
173 2090a03d 2018-09-09 stsp struct got_repository *repo)
175 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
176 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
177 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
179 2090a03d 2018-09-09 stsp char *path_packfile;
181 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
183 2090a03d 2018-09-09 stsp return err;
185 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
187 2090a03d 2018-09-09 stsp return err;
189 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
190 2090a03d 2018-09-09 stsp if (pack == NULL) {
191 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
196 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
200 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
202 2090a03d 2018-09-09 stsp free(path_packfile);
203 4558fcd4 2018-01-14 stsp return err;
206 4558fcd4 2018-01-14 stsp const struct got_error *
207 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
208 4558fcd4 2018-01-14 stsp struct got_object_id *id)
210 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
211 6c00b545 2018-01-17 stsp char *path;
214 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
215 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
216 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
217 7bb0daa1 2018-06-21 stsp return NULL;
220 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
221 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
222 59790a32 2018-09-15 stsp return err;
223 59790a32 2018-09-15 stsp if (*obj) {
224 59790a32 2018-09-15 stsp (*obj)->refcnt++;
225 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
228 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
230 4558fcd4 2018-01-14 stsp return err;
232 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
233 2178c42e 2018-04-22 stsp if (fd == -1) {
234 59790a32 2018-09-15 stsp if (errno == ENOENT)
235 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
237 6c00b545 2018-01-17 stsp err = got_error_from_errno();
240 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
243 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
246 59790a32 2018-09-15 stsp (*obj)->refcnt++;
247 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
249 6c00b545 2018-01-17 stsp free(path);
250 2178c42e 2018-04-22 stsp if (fd != -1)
252 ab9a70b2 2017-11-06 stsp return err;
256 6dfa2fd3 2018-02-12 stsp const struct got_error *
257 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
258 6dfa2fd3 2018-02-12 stsp const char *id_str)
260 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
262 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
263 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
265 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
268 434025f3 2018-09-16 stsp static const struct got_error *
269 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
270 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
272 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
274 434025f3 2018-09-16 stsp if (check_cache) {
275 434025f3 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
276 434025f3 2018-09-16 stsp if (*commit != NULL) {
277 434025f3 2018-09-16 stsp (*commit)->refcnt++;
278 434025f3 2018-09-16 stsp return NULL;
281 434025f3 2018-09-16 stsp *commit = NULL;
283 434025f3 2018-09-16 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
284 434025f3 2018-09-16 stsp return got_error(GOT_ERR_OBJ_TYPE);
286 434025f3 2018-09-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
287 434025f3 2018-09-16 stsp struct got_pack *pack;
288 434025f3 2018-09-16 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
289 434025f3 2018-09-16 stsp if (pack == NULL) {
290 434025f3 2018-09-16 stsp err = got_repo_cache_pack(&pack, repo,
291 434025f3 2018-09-16 stsp obj->path_packfile, NULL);
293 434025f3 2018-09-16 stsp return err;
295 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
298 434025f3 2018-09-16 stsp err = open_loose_object(&fd, obj, repo);
300 434025f3 2018-09-16 stsp return err;
301 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
305 434025f3 2018-09-16 stsp if (err == NULL) {
306 434025f3 2018-09-16 stsp (*commit)->refcnt++;
307 434025f3 2018-09-16 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
310 e32baab7 2018-11-05 stsp return err;
313 e32baab7 2018-11-05 stsp const struct got_error *
314 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
315 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
317 e32baab7 2018-11-05 stsp const struct got_error *err;
318 e32baab7 2018-11-05 stsp struct got_object *obj;
320 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
321 e32baab7 2018-11-05 stsp if (*commit != NULL) {
322 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
323 e32baab7 2018-11-05 stsp return NULL;
326 e32baab7 2018-11-05 stsp err = got_object_open(&obj, repo, id);
328 e32baab7 2018-11-05 stsp return err;
329 e32baab7 2018-11-05 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
330 e32baab7 2018-11-05 stsp err = got_error(GOT_ERR_OBJ_TYPE);
334 e32baab7 2018-11-05 stsp err = open_commit(commit, repo, obj, 0);
336 e32baab7 2018-11-05 stsp got_object_close(obj);
337 7762fe12 2018-11-05 stsp return err;
340 e32baab7 2018-11-05 stsp const struct got_error *
341 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
342 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
344 e32baab7 2018-11-05 stsp return open_commit(commit, repo, obj, 1);
347 434025f3 2018-09-16 stsp const struct got_error *
348 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
350 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
352 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
353 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
354 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
356 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
357 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
358 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
359 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
360 dbc6a6b6 2018-07-12 stsp *qid = NULL;
361 dbc6a6b6 2018-07-12 stsp return err;
364 dbc6a6b6 2018-07-12 stsp return NULL;
367 71eb0e7f 2018-09-16 stsp static const struct got_error *
368 71eb0e7f 2018-09-16 stsp open_tree(struct got_tree_object **tree,
369 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
371 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
373 71eb0e7f 2018-09-16 stsp if (check_cache) {
374 71eb0e7f 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
375 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
376 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
377 71eb0e7f 2018-09-16 stsp return NULL;
380 71eb0e7f 2018-09-16 stsp *tree = NULL;
382 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
383 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
385 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
386 e7885405 2018-09-10 stsp struct got_pack *pack;
387 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
388 e7885405 2018-09-10 stsp if (pack == NULL) {
389 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
390 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
392 e7885405 2018-09-10 stsp return err;
394 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
397 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
399 e0ab43e7 2018-03-16 stsp return err;
400 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
404 f6be5c30 2018-06-22 stsp if (err == NULL) {
405 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
406 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
409 776d4d29 2018-06-17 stsp return err;
412 776d4d29 2018-06-17 stsp const struct got_error *
413 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
414 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
416 776d4d29 2018-06-17 stsp const struct got_error *err;
417 776d4d29 2018-06-17 stsp struct got_object *obj;
419 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
420 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
421 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
422 e8eb494a 2018-09-16 stsp return NULL;
425 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
427 776d4d29 2018-06-17 stsp return err;
428 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
429 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
433 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
435 776d4d29 2018-06-17 stsp got_object_close(obj);
436 0ffeb3c2 2017-11-26 stsp return err;
439 71eb0e7f 2018-09-16 stsp const struct got_error *
440 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
441 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
443 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
446 883f0469 2018-06-23 stsp const struct got_tree_entries *
447 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
449 883f0469 2018-06-23 stsp return &tree->entries;
452 3840f4c9 2018-09-12 stsp static const struct got_error *
453 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
454 3840f4c9 2018-09-12 stsp struct got_pack *pack)
456 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
457 3840f4c9 2018-09-12 stsp int outfd_child;
458 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
460 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
461 3840f4c9 2018-09-12 stsp if (basefd == -1)
462 3840f4c9 2018-09-12 stsp return got_error_from_errno();
463 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
464 3840f4c9 2018-09-12 stsp if (accumfd == -1)
465 3840f4c9 2018-09-12 stsp return got_error_from_errno();
467 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
468 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
469 3840f4c9 2018-09-12 stsp return got_error_from_errno();
471 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
473 3840f4c9 2018-09-12 stsp return err;
475 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
476 3840f4c9 2018-09-12 stsp outfd_child);
478 3840f4c9 2018-09-12 stsp close(outfd_child);
479 3840f4c9 2018-09-12 stsp return err;
481 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
484 3840f4c9 2018-09-12 stsp close(basefd);
485 3840f4c9 2018-09-12 stsp close(accumfd);
486 3840f4c9 2018-09-12 stsp close(outfd_child);
487 3840f4c9 2018-09-12 stsp return err;
490 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
493 3840f4c9 2018-09-12 stsp close(accumfd);
494 3840f4c9 2018-09-12 stsp close(outfd_child);
495 3840f4c9 2018-09-12 stsp return err;
498 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
500 3840f4c9 2018-09-12 stsp return err;
502 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
503 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
505 3840f4c9 2018-09-12 stsp return err;
508 68482ea3 2017-11-27 stsp const struct got_error *
509 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
510 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
512 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
514 55da3778 2018-09-10 stsp size_t size;
515 55da3778 2018-09-10 stsp struct stat sb;
517 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
518 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
520 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
521 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
523 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
524 4558fcd4 2018-01-14 stsp if (*blob == NULL)
525 0a585a0d 2018-03-17 stsp return got_error_from_errno();
527 55da3778 2018-09-10 stsp outfd = got_opentempfd();
528 55da3778 2018-09-10 stsp if (outfd == -1)
529 55da3778 2018-09-10 stsp return got_error_from_errno();
531 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
532 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
533 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
536 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
537 55da3778 2018-09-10 stsp struct got_pack *pack;
538 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
539 55da3778 2018-09-10 stsp if (pack == NULL) {
540 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
541 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
545 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
548 55da3778 2018-09-10 stsp obj->size = size;
552 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
556 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
557 3aca5731 2018-04-24 stsp close(infd);
561 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
562 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
567 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
568 55da3778 2018-09-10 stsp err = got_error_from_errno();
572 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
573 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
577 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
578 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
579 55da3778 2018-09-10 stsp err = got_error_from_errno();
580 55da3778 2018-09-10 stsp close(outfd);
584 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
585 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
586 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
590 55da3778 2018-09-10 stsp if (*blob) {
591 55da3778 2018-09-10 stsp if ((*blob)->f)
592 55da3778 2018-09-10 stsp fclose((*blob)->f);
593 55da3778 2018-09-10 stsp free((*blob)->read_buf);
594 55da3778 2018-09-10 stsp free(*blob);
595 55da3778 2018-09-10 stsp *blob = NULL;
596 55da3778 2018-09-10 stsp } else if (outfd != -1)
597 55da3778 2018-09-10 stsp close(outfd);
599 a19581a2 2018-06-21 stsp return err;
602 a19581a2 2018-06-21 stsp const struct got_error *
603 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
604 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
605 a19581a2 2018-06-21 stsp size_t blocksize)
607 a19581a2 2018-06-21 stsp const struct got_error *err;
608 a19581a2 2018-06-21 stsp struct got_object *obj;
610 835e0dbd 2018-06-21 stsp *blob = NULL;
612 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
614 a19581a2 2018-06-21 stsp return err;
615 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
616 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
620 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
622 a19581a2 2018-06-21 stsp got_object_close(obj);
623 68482ea3 2017-11-27 stsp return err;
627 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
629 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
630 68482ea3 2017-11-27 stsp fclose(blob->f);
631 68482ea3 2017-11-27 stsp free(blob);
635 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
637 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
641 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
643 f934cf2c 2018-02-12 stsp return blob->hdrlen;
646 f934cf2c 2018-02-12 stsp const uint8_t *
647 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
649 f934cf2c 2018-02-12 stsp return blob->read_buf;
652 68482ea3 2017-11-27 stsp const struct got_error *
653 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
657 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
658 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
659 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
660 eb651edf 2018-02-11 stsp *outlenp = n;
661 35e9ba5d 2018-06-21 stsp return NULL;
664 35e9ba5d 2018-06-21 stsp const struct got_error *
665 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
666 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
668 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
669 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
670 84451b3e 2018-07-10 stsp const uint8_t *buf;
673 84451b3e 2018-07-10 stsp if (total_len)
674 84451b3e 2018-07-10 stsp *total_len = 0;
675 84451b3e 2018-07-10 stsp if (nlines)
676 84451b3e 2018-07-10 stsp *nlines = 0;
678 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
680 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
682 35e9ba5d 2018-06-21 stsp return err;
683 35e9ba5d 2018-06-21 stsp if (len == 0)
685 84451b3e 2018-07-10 stsp if (total_len)
686 84451b3e 2018-07-10 stsp *total_len += len;
687 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
688 84451b3e 2018-07-10 stsp if (nlines) {
689 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
690 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
691 84451b3e 2018-07-10 stsp (*nlines)++;
694 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
695 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
696 35e9ba5d 2018-06-21 stsp hdrlen = 0;
697 35e9ba5d 2018-06-21 stsp } while (len != 0);
699 35e9ba5d 2018-06-21 stsp fflush(outfile);
700 35e9ba5d 2018-06-21 stsp rewind(outfile);
702 776d4d29 2018-06-17 stsp return NULL;
705 776d4d29 2018-06-17 stsp static struct got_tree_entry *
706 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
708 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
710 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
711 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
712 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
713 63da309a 2018-11-07 stsp if (cmp < 0)
715 63da309a 2018-11-07 stsp if (cmp > 0)
717 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
720 eb651edf 2018-02-11 stsp return NULL;
723 776d4d29 2018-06-17 stsp const struct got_error *
724 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
725 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
727 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
728 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
729 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
730 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
731 65a9bbe9 2018-09-15 stsp const char *seg, *s;
732 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
734 27d434c2 2018-09-15 stsp *id = NULL;
736 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
737 776d4d29 2018-06-17 stsp if (path[0] != '/')
738 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
740 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
744 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
745 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
746 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
747 27d434c2 2018-09-15 stsp if (*id == NULL)
748 27d434c2 2018-09-15 stsp err = got_error_from_errno();
752 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
757 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
760 65a9bbe9 2018-09-15 stsp seglen = 0;
761 67606321 2018-06-21 stsp while (len > 0) {
762 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
764 776d4d29 2018-06-17 stsp if (*s != '/') {
772 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
773 db37e2c0 2018-06-21 stsp if (te == NULL) {
774 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
778 67606321 2018-06-21 stsp if (len == 0)
781 776d4d29 2018-06-17 stsp seg = s + 1;
782 65a9bbe9 2018-09-15 stsp seglen = 0;
786 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
791 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
792 776d4d29 2018-06-17 stsp tree = next_tree;
797 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
798 27d434c2 2018-09-15 stsp if (*id == NULL)
799 27d434c2 2018-09-15 stsp return got_error_from_errno();
801 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
803 776d4d29 2018-06-17 stsp if (commit)
804 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
806 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
807 776d4d29 2018-06-17 stsp return err;
810 07862c20 2018-09-15 stsp const struct got_error *
811 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
812 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
813 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
815 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
816 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
817 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
818 65a9bbe9 2018-09-15 stsp const char *seg, *s;
819 29696368 2018-11-05 stsp size_t seglen, remain = strlen(path);
821 07862c20 2018-09-15 stsp *changed = 0;
823 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
824 07862c20 2018-09-15 stsp if (path[0] != '/')
825 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
827 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
828 07862c20 2018-09-15 stsp if (path[1] == '\0')
829 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
831 07862c20 2018-09-15 stsp tree1 = tree01;
832 07862c20 2018-09-15 stsp tree2 = tree02;
834 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
837 65a9bbe9 2018-09-15 stsp seglen = 0;
838 29696368 2018-11-05 stsp while (remain > 0) {
839 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
841 07862c20 2018-09-15 stsp if (*s != '/') {
849 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
850 07862c20 2018-09-15 stsp if (te1 == NULL) {
851 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
855 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
856 07862c20 2018-09-15 stsp if (te2 == NULL) {
857 07862c20 2018-09-15 stsp *changed = 1;
861 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
862 07862c20 2018-09-15 stsp *changed = 1;
866 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
867 07862c20 2018-09-15 stsp *changed = 0;
871 29696368 2018-11-05 stsp if (remain == 0) { /* final path element */
872 07862c20 2018-09-15 stsp *changed = 1;
876 07862c20 2018-09-15 stsp seg = s + 1;
879 65a9bbe9 2018-09-15 stsp seglen = 0;
881 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
883 07862c20 2018-09-15 stsp te1 = NULL;
886 a31cea73 2018-09-15 stsp if (tree1 != tree01)
887 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
888 07862c20 2018-09-15 stsp tree1 = next_tree1;
890 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
892 07862c20 2018-09-15 stsp te2 = NULL;
895 a31cea73 2018-09-15 stsp if (tree2 != tree02)
896 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
897 07862c20 2018-09-15 stsp tree2 = next_tree2;
901 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
902 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
903 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
904 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
905 77880158 2018-11-04 stsp return err;
908 77880158 2018-11-04 stsp static void
909 77880158 2018-11-04 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
911 77880158 2018-11-04 stsp close(imsg_fds[0]);
913 77880158 2018-11-04 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
914 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
915 77880158 2018-11-04 stsp strerror(errno));
918 77880158 2018-11-04 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
919 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
920 77880158 2018-11-04 stsp strerror(errno));
924 77880158 2018-11-04 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
925 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
926 77880158 2018-11-04 stsp strerror(errno));
931 77880158 2018-11-04 stsp static const struct got_error *
932 77880158 2018-11-04 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
934 77880158 2018-11-04 stsp const struct got_error *err = NULL;
935 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
937 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
939 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
941 77880158 2018-11-04 stsp return err;
943 77880158 2018-11-04 stsp return got_privsep_recv_obj(obj, ibuf);
946 77880158 2018-11-04 stsp const struct got_error *
947 77880158 2018-11-04 stsp got_object_read_header_privsep(struct got_object **obj,
948 77880158 2018-11-04 stsp struct got_repository *repo, int obj_fd)
950 77880158 2018-11-04 stsp int imsg_fds[2];
952 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
954 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
955 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
957 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
958 77880158 2018-11-04 stsp if (ibuf == NULL)
959 77880158 2018-11-04 stsp return got_error_from_errno();
961 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
962 77880158 2018-11-04 stsp return got_error_from_errno();
964 77880158 2018-11-04 stsp pid = fork();
965 77880158 2018-11-04 stsp if (pid == -1)
966 77880158 2018-11-04 stsp return got_error_from_errno();
967 77880158 2018-11-04 stsp else if (pid == 0) {
968 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
969 77880158 2018-11-04 stsp repo->path);
970 77880158 2018-11-04 stsp /* not reached */
973 77880158 2018-11-04 stsp close(imsg_fds[1]);
974 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
975 77880158 2018-11-04 stsp imsg_fds[0];
976 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
977 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
978 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
980 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
983 77880158 2018-11-04 stsp static const struct got_error *
984 77880158 2018-11-04 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
985 77880158 2018-11-04 stsp struct got_object_id *id)
987 77880158 2018-11-04 stsp const struct got_error *err = NULL;
988 77880158 2018-11-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
990 77880158 2018-11-04 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
992 77880158 2018-11-04 stsp return err;
994 77880158 2018-11-04 stsp err = got_privsep_recv_obj(obj, ibuf);
996 77880158 2018-11-04 stsp return err;
998 77880158 2018-11-04 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
999 77880158 2018-11-04 stsp if ((*obj)->path_packfile == NULL) {
1000 77880158 2018-11-04 stsp err = got_error_from_errno();
1001 77880158 2018-11-04 stsp return err;
1003 77880158 2018-11-04 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1005 77880158 2018-11-04 stsp return NULL;
1008 77880158 2018-11-04 stsp const struct got_error *
1009 77880158 2018-11-04 stsp got_object_packed_read_privsep(struct got_object **obj,
1010 77880158 2018-11-04 stsp struct got_repository *repo, struct got_pack *pack,
1011 77880158 2018-11-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1013 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1014 77880158 2018-11-04 stsp int imsg_fds[2];
1015 77880158 2018-11-04 stsp pid_t pid;
1016 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1018 77880158 2018-11-04 stsp if (pack->privsep_child)
1019 77880158 2018-11-04 stsp return request_packed_object(obj, pack, idx, id);
1021 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1022 77880158 2018-11-04 stsp if (ibuf == NULL)
1023 77880158 2018-11-04 stsp return got_error_from_errno();
1025 77880158 2018-11-04 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1026 77880158 2018-11-04 stsp if (pack->privsep_child == NULL) {
1027 77880158 2018-11-04 stsp err = got_error_from_errno();
1028 77880158 2018-11-04 stsp free(ibuf);
1029 77880158 2018-11-04 stsp return err;
1032 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1033 77880158 2018-11-04 stsp err = got_error_from_errno();
1034 77880158 2018-11-04 stsp goto done;
1037 77880158 2018-11-04 stsp pid = fork();
1038 77880158 2018-11-04 stsp if (pid == -1) {
1039 77880158 2018-11-04 stsp err = got_error_from_errno();
1040 77880158 2018-11-04 stsp goto done;
1041 77880158 2018-11-04 stsp } else if (pid == 0) {
1042 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1043 77880158 2018-11-04 stsp pack->path_packfile);
1044 77880158 2018-11-04 stsp /* not reached */
1047 77880158 2018-11-04 stsp close(imsg_fds[1]);
1048 77880158 2018-11-04 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
1049 77880158 2018-11-04 stsp pack->privsep_child->pid = pid;
1050 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1051 77880158 2018-11-04 stsp pack->privsep_child->ibuf = ibuf;
1053 77880158 2018-11-04 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
1054 77880158 2018-11-04 stsp if (err) {
1055 77880158 2018-11-04 stsp const struct got_error *child_err;
1056 77880158 2018-11-04 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1057 77880158 2018-11-04 stsp child_err = got_privsep_wait_for_child(
1058 77880158 2018-11-04 stsp pack->privsep_child->pid);
1059 77880158 2018-11-04 stsp if (child_err && err == NULL)
1060 77880158 2018-11-04 stsp err = child_err;
1061 77880158 2018-11-04 stsp free(ibuf);
1062 77880158 2018-11-04 stsp free(pack->privsep_child);
1063 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1064 77880158 2018-11-04 stsp return err;
1068 77880158 2018-11-04 stsp if (err) {
1069 77880158 2018-11-04 stsp free(ibuf);
1070 77880158 2018-11-04 stsp free(pack->privsep_child);
1071 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1073 77880158 2018-11-04 stsp err = request_packed_object(obj, pack, idx, id);
1074 07862c20 2018-09-15 stsp return err;
1078 77880158 2018-11-04 stsp static const struct got_error *
1079 77880158 2018-11-04 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
1080 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1082 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1083 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1085 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1087 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1089 77880158 2018-11-04 stsp return err;
1091 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, ibuf);
1094 77880158 2018-11-04 stsp const struct got_error *
1095 77880158 2018-11-04 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1096 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1098 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1100 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1102 77880158 2018-11-04 stsp return err;
1104 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1107 77880158 2018-11-04 stsp const struct got_error *
1108 77880158 2018-11-04 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
1109 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1111 77880158 2018-11-04 stsp int imsg_fds[2];
1112 77880158 2018-11-04 stsp pid_t pid;
1113 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1115 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1116 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1118 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1119 77880158 2018-11-04 stsp if (ibuf == NULL)
1120 77880158 2018-11-04 stsp return got_error_from_errno();
1122 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1123 77880158 2018-11-04 stsp return got_error_from_errno();
1125 77880158 2018-11-04 stsp pid = fork();
1126 77880158 2018-11-04 stsp if (pid == -1)
1127 77880158 2018-11-04 stsp return got_error_from_errno();
1128 77880158 2018-11-04 stsp else if (pid == 0) {
1129 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1130 77880158 2018-11-04 stsp repo->path);
1131 77880158 2018-11-04 stsp /* not reached */
1134 77880158 2018-11-04 stsp close(imsg_fds[1]);
1135 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1136 77880158 2018-11-04 stsp imsg_fds[0];
1137 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1138 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1139 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1141 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1144 77880158 2018-11-04 stsp static const struct got_error *
1145 77880158 2018-11-04 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1146 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1148 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1149 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1151 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1153 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1155 77880158 2018-11-04 stsp return err;
1157 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, ibuf);
1160 77880158 2018-11-04 stsp const struct got_error *
1161 77880158 2018-11-04 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
1162 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1164 77880158 2018-11-04 stsp int imsg_fds[2];
1165 77880158 2018-11-04 stsp pid_t pid;
1166 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1168 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1169 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1171 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1172 77880158 2018-11-04 stsp if (ibuf == NULL)
1173 77880158 2018-11-04 stsp return got_error_from_errno();
1175 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1176 77880158 2018-11-04 stsp return got_error_from_errno();
1178 77880158 2018-11-04 stsp pid = fork();
1179 77880158 2018-11-04 stsp if (pid == -1)
1180 77880158 2018-11-04 stsp return got_error_from_errno();
1181 77880158 2018-11-04 stsp else if (pid == 0) {
1182 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1183 77880158 2018-11-04 stsp repo->path);
1184 77880158 2018-11-04 stsp /* not reached */
1187 77880158 2018-11-04 stsp close(imsg_fds[1]);
1189 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1190 77880158 2018-11-04 stsp imsg_fds[0];
1191 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1192 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1193 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1196 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1199 77880158 2018-11-04 stsp const struct got_error *
1200 77880158 2018-11-04 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1201 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1203 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1205 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1207 77880158 2018-11-04 stsp return err;
1209 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1212 77880158 2018-11-04 stsp static const struct got_error *
1213 77880158 2018-11-04 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1215 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1216 77880158 2018-11-04 stsp int outfd_child;
1218 77880158 2018-11-04 stsp outfd_child = dup(outfd);
1219 77880158 2018-11-04 stsp if (outfd_child == -1)
1220 77880158 2018-11-04 stsp return got_error_from_errno();
1222 77880158 2018-11-04 stsp err = got_privsep_send_blob_req(ibuf, infd);
1224 77880158 2018-11-04 stsp return err;
1226 77880158 2018-11-04 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1227 77880158 2018-11-04 stsp if (err) {
1228 77880158 2018-11-04 stsp close(outfd_child);
1229 77880158 2018-11-04 stsp return err;
1232 77880158 2018-11-04 stsp err = got_privsep_recv_blob(size, ibuf);
1234 77880158 2018-11-04 stsp return err;
1236 77880158 2018-11-04 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1237 77880158 2018-11-04 stsp return got_error_from_errno();
1239 77880158 2018-11-04 stsp return err;
1242 77880158 2018-11-04 stsp const struct got_error *
1243 77880158 2018-11-04 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1244 77880158 2018-11-04 stsp struct got_repository *repo)
1246 77880158 2018-11-04 stsp int imsg_fds[2];
1247 77880158 2018-11-04 stsp pid_t pid;
1248 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1250 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1251 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1252 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1255 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1256 77880158 2018-11-04 stsp if (ibuf == NULL)
1257 77880158 2018-11-04 stsp return got_error_from_errno();
1259 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1260 77880158 2018-11-04 stsp return got_error_from_errno();
1262 77880158 2018-11-04 stsp pid = fork();
1263 77880158 2018-11-04 stsp if (pid == -1)
1264 77880158 2018-11-04 stsp return got_error_from_errno();
1265 77880158 2018-11-04 stsp else if (pid == 0) {
1266 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1267 77880158 2018-11-04 stsp repo->path);
1268 77880158 2018-11-04 stsp /* not reached */
1271 77880158 2018-11-04 stsp close(imsg_fds[1]);
1272 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1273 77880158 2018-11-04 stsp imsg_fds[0];
1274 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1275 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1276 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1278 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);