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 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 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
51 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
52 15a94983 2018-12-23 stsp #include "got_lib_pack.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);
83 15a94983 2018-12-23 stsp const struct got_error *
84 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
85 15a94983 2018-12-23 stsp struct got_object_id *id)
87 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
88 15a94983 2018-12-23 stsp struct got_object *obj;
90 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
94 b107e67f 2018-01-19 stsp switch (obj->type) {
95 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
96 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
97 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
98 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
99 15a94983 2018-12-23 stsp *type = obj->type;
102 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
106 15a94983 2018-12-23 stsp got_object_close(obj);
107 15a94983 2018-12-23 stsp return err;
110 ab9a70b2 2017-11-06 stsp static const struct got_error *
111 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
113 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
114 7a132809 2018-07-23 stsp char *hex = NULL;
115 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
117 e6b1056e 2018-04-22 stsp *path = NULL;
119 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
120 0a585a0d 2018-03-17 stsp return got_error_from_errno();
122 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
126 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
127 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
128 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
132 d1cda826 2017-11-06 stsp free(path_objects);
133 d1cda826 2017-11-06 stsp return err;
136 4ee4114f 2018-01-23 stsp static const struct got_error *
137 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
138 4796fb13 2018-12-23 stsp struct got_repository *repo)
140 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
141 a1fd68d8 2018-01-12 stsp char *path;
143 4796fb13 2018-12-23 stsp err = object_path(&path, id, repo);
145 d1cda826 2017-11-06 stsp return err;
146 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
147 d5003b79 2018-04-22 stsp if (*fd == -1) {
148 6c00b545 2018-01-17 stsp err = got_error_from_errno();
152 4558fcd4 2018-01-14 stsp free(path);
153 2090a03d 2018-09-09 stsp return err;
156 2090a03d 2018-09-09 stsp static const struct got_error *
157 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
159 2090a03d 2018-09-09 stsp size_t size;
161 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
162 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
163 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
164 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
166 08451938 2018-11-05 stsp *path_packfile = malloc(size);
167 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
168 2090a03d 2018-09-09 stsp return got_error_from_errno();
170 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
171 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
172 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
173 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
175 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
176 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
178 2090a03d 2018-09-09 stsp return NULL;
181 2090a03d 2018-09-09 stsp static const struct got_error *
182 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
183 2090a03d 2018-09-09 stsp struct got_repository *repo)
185 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
186 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
187 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
189 2090a03d 2018-09-09 stsp char *path_packfile;
191 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
193 2090a03d 2018-09-09 stsp return err;
195 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
197 2090a03d 2018-09-09 stsp return err;
199 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
200 2090a03d 2018-09-09 stsp if (pack == NULL) {
201 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
206 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
210 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
212 2090a03d 2018-09-09 stsp free(path_packfile);
213 4558fcd4 2018-01-14 stsp return err;
216 4558fcd4 2018-01-14 stsp const struct got_error *
217 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
218 4558fcd4 2018-01-14 stsp struct got_object_id *id)
220 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
221 6c00b545 2018-01-17 stsp char *path;
224 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
225 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
226 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
227 7bb0daa1 2018-06-21 stsp return NULL;
230 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
231 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
232 59790a32 2018-09-15 stsp return err;
233 59790a32 2018-09-15 stsp if (*obj) {
234 59790a32 2018-09-15 stsp (*obj)->refcnt++;
235 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
238 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
240 4558fcd4 2018-01-14 stsp return err;
242 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
243 2178c42e 2018-04-22 stsp if (fd == -1) {
244 59790a32 2018-09-15 stsp if (errno == ENOENT)
245 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
247 6c00b545 2018-01-17 stsp err = got_error_from_errno();
250 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
253 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
256 59790a32 2018-09-15 stsp (*obj)->refcnt++;
257 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
259 6c00b545 2018-01-17 stsp free(path);
260 2178c42e 2018-04-22 stsp if (fd != -1)
262 ab9a70b2 2017-11-06 stsp return err;
266 6dfa2fd3 2018-02-12 stsp const struct got_error *
267 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
268 6dfa2fd3 2018-02-12 stsp const char *id_str)
270 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
272 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
273 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
275 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
278 15a94983 2018-12-23 stsp const struct got_error *
279 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
280 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
282 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
283 15a94983 2018-12-23 stsp struct got_object *obj;
285 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
287 15a94983 2018-12-23 stsp return err;
289 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
290 15a94983 2018-12-23 stsp got_object_close(obj);
291 15a94983 2018-12-23 stsp if (*id == NULL)
292 15a94983 2018-12-23 stsp return got_error_from_errno();
294 15a94983 2018-12-23 stsp return NULL;
297 434025f3 2018-09-16 stsp static const struct got_error *
298 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
299 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
301 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
303 434025f3 2018-09-16 stsp if (check_cache) {
304 434025f3 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
305 434025f3 2018-09-16 stsp if (*commit != NULL) {
306 434025f3 2018-09-16 stsp (*commit)->refcnt++;
307 434025f3 2018-09-16 stsp return NULL;
310 434025f3 2018-09-16 stsp *commit = NULL;
312 434025f3 2018-09-16 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
313 434025f3 2018-09-16 stsp return got_error(GOT_ERR_OBJ_TYPE);
315 434025f3 2018-09-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
316 434025f3 2018-09-16 stsp struct got_pack *pack;
317 434025f3 2018-09-16 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
318 434025f3 2018-09-16 stsp if (pack == NULL) {
319 434025f3 2018-09-16 stsp err = got_repo_cache_pack(&pack, repo,
320 434025f3 2018-09-16 stsp obj->path_packfile, NULL);
322 434025f3 2018-09-16 stsp return err;
324 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
327 4796fb13 2018-12-23 stsp err = open_loose_object(&fd, got_object_get_id(obj), repo);
329 434025f3 2018-09-16 stsp return err;
330 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
334 434025f3 2018-09-16 stsp if (err == NULL) {
335 434025f3 2018-09-16 stsp (*commit)->refcnt++;
336 434025f3 2018-09-16 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
339 e32baab7 2018-11-05 stsp return err;
342 e32baab7 2018-11-05 stsp const struct got_error *
343 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
344 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
346 e32baab7 2018-11-05 stsp const struct got_error *err;
347 e32baab7 2018-11-05 stsp struct got_object *obj;
349 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
350 e32baab7 2018-11-05 stsp if (*commit != NULL) {
351 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
352 e32baab7 2018-11-05 stsp return NULL;
355 e32baab7 2018-11-05 stsp err = got_object_open(&obj, repo, id);
357 e32baab7 2018-11-05 stsp return err;
358 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
359 e32baab7 2018-11-05 stsp err = got_error(GOT_ERR_OBJ_TYPE);
363 e32baab7 2018-11-05 stsp err = open_commit(commit, repo, obj, 0);
365 e32baab7 2018-11-05 stsp got_object_close(obj);
366 7762fe12 2018-11-05 stsp return err;
369 e32baab7 2018-11-05 stsp const struct got_error *
370 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
371 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
373 e32baab7 2018-11-05 stsp return open_commit(commit, repo, obj, 1);
376 434025f3 2018-09-16 stsp const struct got_error *
377 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
379 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
381 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
382 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
383 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
385 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
386 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
387 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
388 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
389 dbc6a6b6 2018-07-12 stsp *qid = NULL;
390 dbc6a6b6 2018-07-12 stsp return err;
393 dbc6a6b6 2018-07-12 stsp return NULL;
396 71eb0e7f 2018-09-16 stsp static const struct got_error *
397 71eb0e7f 2018-09-16 stsp open_tree(struct got_tree_object **tree,
398 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
400 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
402 71eb0e7f 2018-09-16 stsp if (check_cache) {
403 71eb0e7f 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
404 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
405 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
406 71eb0e7f 2018-09-16 stsp return NULL;
409 71eb0e7f 2018-09-16 stsp *tree = NULL;
411 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
412 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
414 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
415 e7885405 2018-09-10 stsp struct got_pack *pack;
416 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
417 e7885405 2018-09-10 stsp if (pack == NULL) {
418 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
419 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
421 e7885405 2018-09-10 stsp return err;
423 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
426 4796fb13 2018-12-23 stsp err = open_loose_object(&fd, got_object_get_id(obj), repo);
428 e0ab43e7 2018-03-16 stsp return err;
429 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
433 f6be5c30 2018-06-22 stsp if (err == NULL) {
434 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
435 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
438 776d4d29 2018-06-17 stsp return err;
441 776d4d29 2018-06-17 stsp const struct got_error *
442 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
443 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
445 776d4d29 2018-06-17 stsp const struct got_error *err;
446 776d4d29 2018-06-17 stsp struct got_object *obj;
448 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
449 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
450 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
451 e8eb494a 2018-09-16 stsp return NULL;
454 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
456 776d4d29 2018-06-17 stsp return err;
457 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_TREE) {
458 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
462 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
464 776d4d29 2018-06-17 stsp got_object_close(obj);
465 0ffeb3c2 2017-11-26 stsp return err;
468 71eb0e7f 2018-09-16 stsp const struct got_error *
469 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
470 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
472 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
475 883f0469 2018-06-23 stsp const struct got_tree_entries *
476 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
478 883f0469 2018-06-23 stsp return &tree->entries;
481 3840f4c9 2018-09-12 stsp static const struct got_error *
482 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
483 3840f4c9 2018-09-12 stsp struct got_pack *pack)
485 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
486 3840f4c9 2018-09-12 stsp int outfd_child;
487 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
489 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
490 3840f4c9 2018-09-12 stsp if (basefd == -1)
491 3840f4c9 2018-09-12 stsp return got_error_from_errno();
492 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
493 3840f4c9 2018-09-12 stsp if (accumfd == -1)
494 3840f4c9 2018-09-12 stsp return got_error_from_errno();
496 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
497 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
498 3840f4c9 2018-09-12 stsp return got_error_from_errno();
500 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
502 3840f4c9 2018-09-12 stsp return err;
504 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
505 3840f4c9 2018-09-12 stsp outfd_child);
507 3840f4c9 2018-09-12 stsp close(outfd_child);
508 3840f4c9 2018-09-12 stsp return err;
510 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
513 3840f4c9 2018-09-12 stsp close(basefd);
514 3840f4c9 2018-09-12 stsp close(accumfd);
515 3840f4c9 2018-09-12 stsp close(outfd_child);
516 3840f4c9 2018-09-12 stsp return err;
519 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
522 3840f4c9 2018-09-12 stsp close(accumfd);
523 3840f4c9 2018-09-12 stsp close(outfd_child);
524 3840f4c9 2018-09-12 stsp return err;
527 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
529 3840f4c9 2018-09-12 stsp return err;
531 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
532 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
534 3840f4c9 2018-09-12 stsp return err;
537 68482ea3 2017-11-27 stsp const struct got_error *
538 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
539 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
541 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
543 55da3778 2018-09-10 stsp size_t size;
544 55da3778 2018-09-10 stsp struct stat sb;
546 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
547 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
549 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
550 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
552 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
553 4558fcd4 2018-01-14 stsp if (*blob == NULL)
554 0a585a0d 2018-03-17 stsp return got_error_from_errno();
556 55da3778 2018-09-10 stsp outfd = got_opentempfd();
557 55da3778 2018-09-10 stsp if (outfd == -1)
558 55da3778 2018-09-10 stsp return got_error_from_errno();
560 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
561 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
562 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
565 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
566 55da3778 2018-09-10 stsp struct got_pack *pack;
567 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
568 55da3778 2018-09-10 stsp if (pack == NULL) {
569 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
570 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
574 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
577 55da3778 2018-09-10 stsp obj->size = size;
581 4796fb13 2018-12-23 stsp err = open_loose_object(&infd, got_object_get_id(obj), repo);
585 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
586 3aca5731 2018-04-24 stsp close(infd);
590 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
591 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
596 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
597 55da3778 2018-09-10 stsp err = got_error_from_errno();
601 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
602 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
606 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
607 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
608 55da3778 2018-09-10 stsp err = got_error_from_errno();
609 55da3778 2018-09-10 stsp close(outfd);
613 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
614 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
615 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
619 55da3778 2018-09-10 stsp if (*blob) {
620 55da3778 2018-09-10 stsp if ((*blob)->f)
621 55da3778 2018-09-10 stsp fclose((*blob)->f);
622 55da3778 2018-09-10 stsp free((*blob)->read_buf);
623 55da3778 2018-09-10 stsp free(*blob);
624 55da3778 2018-09-10 stsp *blob = NULL;
625 55da3778 2018-09-10 stsp } else if (outfd != -1)
626 55da3778 2018-09-10 stsp close(outfd);
628 a19581a2 2018-06-21 stsp return err;
631 a19581a2 2018-06-21 stsp const struct got_error *
632 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
633 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
634 a19581a2 2018-06-21 stsp size_t blocksize)
636 a19581a2 2018-06-21 stsp const struct got_error *err;
637 a19581a2 2018-06-21 stsp struct got_object *obj;
639 835e0dbd 2018-06-21 stsp *blob = NULL;
641 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
643 a19581a2 2018-06-21 stsp return err;
644 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
645 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
649 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
651 a19581a2 2018-06-21 stsp got_object_close(obj);
652 68482ea3 2017-11-27 stsp return err;
656 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
658 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
659 68482ea3 2017-11-27 stsp fclose(blob->f);
660 68482ea3 2017-11-27 stsp free(blob);
664 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
666 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
670 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
672 f934cf2c 2018-02-12 stsp return blob->hdrlen;
675 f934cf2c 2018-02-12 stsp const uint8_t *
676 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
678 f934cf2c 2018-02-12 stsp return blob->read_buf;
681 68482ea3 2017-11-27 stsp const struct got_error *
682 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
686 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
687 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
688 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
689 eb651edf 2018-02-11 stsp *outlenp = n;
690 35e9ba5d 2018-06-21 stsp return NULL;
693 35e9ba5d 2018-06-21 stsp const struct got_error *
694 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
695 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
697 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
698 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
699 84451b3e 2018-07-10 stsp const uint8_t *buf;
702 84451b3e 2018-07-10 stsp if (total_len)
703 84451b3e 2018-07-10 stsp *total_len = 0;
704 84451b3e 2018-07-10 stsp if (nlines)
705 84451b3e 2018-07-10 stsp *nlines = 0;
707 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
709 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
711 35e9ba5d 2018-06-21 stsp return err;
712 35e9ba5d 2018-06-21 stsp if (len == 0)
714 84451b3e 2018-07-10 stsp if (total_len)
715 84451b3e 2018-07-10 stsp *total_len += len;
716 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
717 84451b3e 2018-07-10 stsp if (nlines) {
718 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
719 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
720 84451b3e 2018-07-10 stsp (*nlines)++;
723 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
724 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
725 35e9ba5d 2018-06-21 stsp hdrlen = 0;
726 35e9ba5d 2018-06-21 stsp } while (len != 0);
728 35e9ba5d 2018-06-21 stsp fflush(outfile);
729 35e9ba5d 2018-06-21 stsp rewind(outfile);
731 776d4d29 2018-06-17 stsp return NULL;
734 f4a881ce 2018-11-17 stsp static const struct got_error *
735 f4a881ce 2018-11-17 stsp open_tag(struct got_tag_object **tag,
736 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
738 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
740 f4a881ce 2018-11-17 stsp if (check_cache) {
741 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, &obj->id);
742 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
743 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
744 f4a881ce 2018-11-17 stsp return NULL;
747 f4a881ce 2018-11-17 stsp *tag = NULL;
749 f4a881ce 2018-11-17 stsp if (obj->type != GOT_OBJ_TYPE_TAG)
750 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_OBJ_TYPE);
752 f4a881ce 2018-11-17 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
753 f4a881ce 2018-11-17 stsp struct got_pack *pack;
754 f4a881ce 2018-11-17 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
755 f4a881ce 2018-11-17 stsp if (pack == NULL) {
756 f4a881ce 2018-11-17 stsp err = got_repo_cache_pack(&pack, repo,
757 f4a881ce 2018-11-17 stsp obj->path_packfile, NULL);
759 f4a881ce 2018-11-17 stsp return err;
761 f4a881ce 2018-11-17 stsp err = got_object_read_packed_tag_privsep(tag, obj, pack);
764 4796fb13 2018-12-23 stsp err = open_loose_object(&fd, got_object_get_id(obj), repo);
766 f4a881ce 2018-11-17 stsp return err;
767 f4a881ce 2018-11-17 stsp err = got_object_read_tag_privsep(tag, obj, fd, repo);
771 f4a881ce 2018-11-17 stsp if (err == NULL) {
772 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
773 f4a881ce 2018-11-17 stsp err = got_repo_cache_tag(repo, &obj->id, *tag);
776 f4a881ce 2018-11-17 stsp return err;
779 f4a881ce 2018-11-17 stsp const struct got_error *
780 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
781 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
783 f4a881ce 2018-11-17 stsp const struct got_error *err;
784 f4a881ce 2018-11-17 stsp struct got_object *obj;
786 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
787 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
788 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
789 f4a881ce 2018-11-17 stsp return NULL;
792 f4a881ce 2018-11-17 stsp err = got_object_open(&obj, repo, id);
794 f4a881ce 2018-11-17 stsp return err;
795 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
796 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
800 f4a881ce 2018-11-17 stsp err = open_tag(tag, repo, obj, 0);
802 f4a881ce 2018-11-17 stsp got_object_close(obj);
803 f4a881ce 2018-11-17 stsp return err;
806 f4a881ce 2018-11-17 stsp const struct got_error *
807 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
808 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
810 f4a881ce 2018-11-17 stsp return open_tag(tag, repo, obj, 1);
813 776d4d29 2018-06-17 stsp static struct got_tree_entry *
814 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
816 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
818 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
819 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
820 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
821 63da309a 2018-11-07 stsp if (cmp < 0)
823 63da309a 2018-11-07 stsp if (cmp > 0)
825 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
828 eb651edf 2018-02-11 stsp return NULL;
831 776d4d29 2018-06-17 stsp const struct got_error *
832 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
833 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
835 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
836 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
837 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
838 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
839 65a9bbe9 2018-09-15 stsp const char *seg, *s;
840 b7cd37e5 2018-11-18 stsp size_t seglen;
842 27d434c2 2018-09-15 stsp *id = NULL;
844 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
845 776d4d29 2018-06-17 stsp if (path[0] != '/')
846 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
848 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
852 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
853 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
854 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
855 27d434c2 2018-09-15 stsp if (*id == NULL)
856 27d434c2 2018-09-15 stsp err = got_error_from_errno();
860 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
865 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
867 65a9bbe9 2018-09-15 stsp seglen = 0;
868 b7cd37e5 2018-11-18 stsp while (*s) {
869 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
871 776d4d29 2018-06-17 stsp if (*s != '/') {
878 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
879 db37e2c0 2018-06-21 stsp if (te == NULL) {
880 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
884 b7cd37e5 2018-11-18 stsp if (*s == '\0')
887 776d4d29 2018-06-17 stsp seg = s + 1;
888 65a9bbe9 2018-09-15 stsp seglen = 0;
891 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
896 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
897 776d4d29 2018-06-17 stsp tree = next_tree;
902 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
903 27d434c2 2018-09-15 stsp if (*id == NULL)
904 27d434c2 2018-09-15 stsp return got_error_from_errno();
906 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
908 776d4d29 2018-06-17 stsp if (commit)
909 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
911 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
912 776d4d29 2018-06-17 stsp return err;
915 07862c20 2018-09-15 stsp const struct got_error *
916 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
917 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
918 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
920 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
921 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
922 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
923 65a9bbe9 2018-09-15 stsp const char *seg, *s;
924 3b7f9878 2018-11-18 stsp size_t seglen;
926 07862c20 2018-09-15 stsp *changed = 0;
928 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
929 07862c20 2018-09-15 stsp if (path[0] != '/')
930 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
932 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
933 07862c20 2018-09-15 stsp if (path[1] == '\0')
934 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
936 07862c20 2018-09-15 stsp tree1 = tree01;
937 07862c20 2018-09-15 stsp tree2 = tree02;
939 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
941 65a9bbe9 2018-09-15 stsp seglen = 0;
942 3b7f9878 2018-11-18 stsp while (*s) {
943 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
945 07862c20 2018-09-15 stsp if (*s != '/') {
952 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
953 07862c20 2018-09-15 stsp if (te1 == NULL) {
954 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
958 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
959 07862c20 2018-09-15 stsp if (te2 == NULL) {
960 07862c20 2018-09-15 stsp *changed = 1;
964 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
965 07862c20 2018-09-15 stsp *changed = 1;
969 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
970 07862c20 2018-09-15 stsp *changed = 0;
974 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
975 07862c20 2018-09-15 stsp *changed = 1;
979 07862c20 2018-09-15 stsp seg = s + 1;
981 65a9bbe9 2018-09-15 stsp seglen = 0;
983 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
985 07862c20 2018-09-15 stsp te1 = NULL;
988 a31cea73 2018-09-15 stsp if (tree1 != tree01)
989 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
990 07862c20 2018-09-15 stsp tree1 = next_tree1;
992 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
994 07862c20 2018-09-15 stsp te2 = NULL;
997 a31cea73 2018-09-15 stsp if (tree2 != tree02)
998 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
999 07862c20 2018-09-15 stsp tree2 = next_tree2;
1003 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1004 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1005 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1006 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1007 77880158 2018-11-04 stsp return err;
1010 77880158 2018-11-04 stsp static void
1011 77880158 2018-11-04 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
1013 77880158 2018-11-04 stsp close(imsg_fds[0]);
1015 77880158 2018-11-04 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1016 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
1017 77880158 2018-11-04 stsp strerror(errno));
1020 77880158 2018-11-04 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1021 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
1022 77880158 2018-11-04 stsp strerror(errno));
1026 77880158 2018-11-04 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
1027 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1028 77880158 2018-11-04 stsp strerror(errno));
1033 77880158 2018-11-04 stsp static const struct got_error *
1034 77880158 2018-11-04 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
1036 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1037 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1039 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1041 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
1043 77880158 2018-11-04 stsp return err;
1045 77880158 2018-11-04 stsp return got_privsep_recv_obj(obj, ibuf);
1048 77880158 2018-11-04 stsp const struct got_error *
1049 77880158 2018-11-04 stsp got_object_read_header_privsep(struct got_object **obj,
1050 77880158 2018-11-04 stsp struct got_repository *repo, int obj_fd)
1052 77880158 2018-11-04 stsp int imsg_fds[2];
1053 77880158 2018-11-04 stsp pid_t pid;
1054 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1056 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1057 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1059 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1060 77880158 2018-11-04 stsp if (ibuf == NULL)
1061 77880158 2018-11-04 stsp return got_error_from_errno();
1063 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1064 77880158 2018-11-04 stsp return got_error_from_errno();
1066 77880158 2018-11-04 stsp pid = fork();
1067 77880158 2018-11-04 stsp if (pid == -1)
1068 77880158 2018-11-04 stsp return got_error_from_errno();
1069 77880158 2018-11-04 stsp else if (pid == 0) {
1070 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1071 77880158 2018-11-04 stsp repo->path);
1072 77880158 2018-11-04 stsp /* not reached */
1075 77880158 2018-11-04 stsp close(imsg_fds[1]);
1076 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1077 77880158 2018-11-04 stsp imsg_fds[0];
1078 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1079 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1080 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1082 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1085 77880158 2018-11-04 stsp static const struct got_error *
1086 77880158 2018-11-04 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1087 77880158 2018-11-04 stsp struct got_object_id *id)
1089 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1090 77880158 2018-11-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1092 77880158 2018-11-04 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1094 77880158 2018-11-04 stsp return err;
1096 77880158 2018-11-04 stsp err = got_privsep_recv_obj(obj, ibuf);
1098 77880158 2018-11-04 stsp return err;
1100 77880158 2018-11-04 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1101 77880158 2018-11-04 stsp if ((*obj)->path_packfile == NULL) {
1102 77880158 2018-11-04 stsp err = got_error_from_errno();
1103 77880158 2018-11-04 stsp return err;
1105 77880158 2018-11-04 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1107 77880158 2018-11-04 stsp return NULL;
1110 77880158 2018-11-04 stsp const struct got_error *
1111 77880158 2018-11-04 stsp got_object_packed_read_privsep(struct got_object **obj,
1112 77880158 2018-11-04 stsp struct got_repository *repo, struct got_pack *pack,
1113 77880158 2018-11-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1115 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1116 77880158 2018-11-04 stsp int imsg_fds[2];
1117 77880158 2018-11-04 stsp pid_t pid;
1118 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1120 77880158 2018-11-04 stsp if (pack->privsep_child)
1121 77880158 2018-11-04 stsp return request_packed_object(obj, pack, idx, id);
1123 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1124 77880158 2018-11-04 stsp if (ibuf == NULL)
1125 77880158 2018-11-04 stsp return got_error_from_errno();
1127 77880158 2018-11-04 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1128 77880158 2018-11-04 stsp if (pack->privsep_child == NULL) {
1129 77880158 2018-11-04 stsp err = got_error_from_errno();
1130 77880158 2018-11-04 stsp free(ibuf);
1131 77880158 2018-11-04 stsp return err;
1134 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1135 77880158 2018-11-04 stsp err = got_error_from_errno();
1136 77880158 2018-11-04 stsp goto done;
1139 77880158 2018-11-04 stsp pid = fork();
1140 77880158 2018-11-04 stsp if (pid == -1) {
1141 77880158 2018-11-04 stsp err = got_error_from_errno();
1142 77880158 2018-11-04 stsp goto done;
1143 77880158 2018-11-04 stsp } else if (pid == 0) {
1144 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1145 77880158 2018-11-04 stsp pack->path_packfile);
1146 77880158 2018-11-04 stsp /* not reached */
1149 77880158 2018-11-04 stsp close(imsg_fds[1]);
1150 77880158 2018-11-04 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
1151 77880158 2018-11-04 stsp pack->privsep_child->pid = pid;
1152 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1153 77880158 2018-11-04 stsp pack->privsep_child->ibuf = ibuf;
1155 77880158 2018-11-04 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
1156 77880158 2018-11-04 stsp if (err) {
1157 77880158 2018-11-04 stsp const struct got_error *child_err;
1158 77880158 2018-11-04 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1159 77880158 2018-11-04 stsp child_err = got_privsep_wait_for_child(
1160 77880158 2018-11-04 stsp pack->privsep_child->pid);
1161 77880158 2018-11-04 stsp if (child_err && err == NULL)
1162 77880158 2018-11-04 stsp err = child_err;
1163 77880158 2018-11-04 stsp free(ibuf);
1164 77880158 2018-11-04 stsp free(pack->privsep_child);
1165 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1166 77880158 2018-11-04 stsp return err;
1170 77880158 2018-11-04 stsp if (err) {
1171 77880158 2018-11-04 stsp free(ibuf);
1172 77880158 2018-11-04 stsp free(pack->privsep_child);
1173 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1175 77880158 2018-11-04 stsp err = request_packed_object(obj, pack, idx, id);
1176 07862c20 2018-09-15 stsp return err;
1179 77880158 2018-11-04 stsp static const struct got_error *
1180 77880158 2018-11-04 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
1181 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1183 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1184 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1186 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1188 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1190 77880158 2018-11-04 stsp return err;
1192 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, ibuf);
1195 77880158 2018-11-04 stsp const struct got_error *
1196 77880158 2018-11-04 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1197 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1199 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1201 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1203 77880158 2018-11-04 stsp return err;
1205 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1208 77880158 2018-11-04 stsp const struct got_error *
1209 77880158 2018-11-04 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
1210 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1212 77880158 2018-11-04 stsp int imsg_fds[2];
1213 77880158 2018-11-04 stsp pid_t pid;
1214 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1216 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1217 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1219 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1220 77880158 2018-11-04 stsp if (ibuf == NULL)
1221 77880158 2018-11-04 stsp return got_error_from_errno();
1223 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1224 77880158 2018-11-04 stsp return got_error_from_errno();
1226 77880158 2018-11-04 stsp pid = fork();
1227 77880158 2018-11-04 stsp if (pid == -1)
1228 77880158 2018-11-04 stsp return got_error_from_errno();
1229 77880158 2018-11-04 stsp else if (pid == 0) {
1230 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1231 77880158 2018-11-04 stsp repo->path);
1232 77880158 2018-11-04 stsp /* not reached */
1235 77880158 2018-11-04 stsp close(imsg_fds[1]);
1236 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1237 77880158 2018-11-04 stsp imsg_fds[0];
1238 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1239 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1240 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1242 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1245 77880158 2018-11-04 stsp static const struct got_error *
1246 77880158 2018-11-04 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1247 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1249 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1250 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1252 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1254 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1256 77880158 2018-11-04 stsp return err;
1258 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, ibuf);
1261 77880158 2018-11-04 stsp const struct got_error *
1262 77880158 2018-11-04 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
1263 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1265 77880158 2018-11-04 stsp int imsg_fds[2];
1266 77880158 2018-11-04 stsp pid_t pid;
1267 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1269 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1270 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1272 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1273 77880158 2018-11-04 stsp if (ibuf == NULL)
1274 77880158 2018-11-04 stsp return got_error_from_errno();
1276 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1277 77880158 2018-11-04 stsp return got_error_from_errno();
1279 77880158 2018-11-04 stsp pid = fork();
1280 77880158 2018-11-04 stsp if (pid == -1)
1281 77880158 2018-11-04 stsp return got_error_from_errno();
1282 77880158 2018-11-04 stsp else if (pid == 0) {
1283 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1284 77880158 2018-11-04 stsp repo->path);
1285 77880158 2018-11-04 stsp /* not reached */
1288 77880158 2018-11-04 stsp close(imsg_fds[1]);
1290 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1291 77880158 2018-11-04 stsp imsg_fds[0];
1292 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1293 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1294 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1297 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1300 77880158 2018-11-04 stsp const struct got_error *
1301 77880158 2018-11-04 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1302 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1304 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1306 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1308 77880158 2018-11-04 stsp return err;
1310 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1313 77880158 2018-11-04 stsp static const struct got_error *
1314 77880158 2018-11-04 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1316 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1317 77880158 2018-11-04 stsp int outfd_child;
1319 77880158 2018-11-04 stsp outfd_child = dup(outfd);
1320 77880158 2018-11-04 stsp if (outfd_child == -1)
1321 77880158 2018-11-04 stsp return got_error_from_errno();
1323 77880158 2018-11-04 stsp err = got_privsep_send_blob_req(ibuf, infd);
1325 77880158 2018-11-04 stsp return err;
1327 77880158 2018-11-04 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1328 77880158 2018-11-04 stsp if (err) {
1329 77880158 2018-11-04 stsp close(outfd_child);
1330 77880158 2018-11-04 stsp return err;
1333 77880158 2018-11-04 stsp err = got_privsep_recv_blob(size, ibuf);
1335 77880158 2018-11-04 stsp return err;
1337 77880158 2018-11-04 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1338 77880158 2018-11-04 stsp return got_error_from_errno();
1340 77880158 2018-11-04 stsp return err;
1343 77880158 2018-11-04 stsp const struct got_error *
1344 77880158 2018-11-04 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1345 77880158 2018-11-04 stsp struct got_repository *repo)
1347 77880158 2018-11-04 stsp int imsg_fds[2];
1348 77880158 2018-11-04 stsp pid_t pid;
1349 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1351 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1352 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1353 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1356 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1357 77880158 2018-11-04 stsp if (ibuf == NULL)
1358 77880158 2018-11-04 stsp return got_error_from_errno();
1360 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1361 77880158 2018-11-04 stsp return got_error_from_errno();
1363 77880158 2018-11-04 stsp pid = fork();
1364 77880158 2018-11-04 stsp if (pid == -1)
1365 77880158 2018-11-04 stsp return got_error_from_errno();
1366 77880158 2018-11-04 stsp else if (pid == 0) {
1367 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1368 77880158 2018-11-04 stsp repo->path);
1369 77880158 2018-11-04 stsp /* not reached */
1372 77880158 2018-11-04 stsp close(imsg_fds[1]);
1373 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1374 77880158 2018-11-04 stsp imsg_fds[0];
1375 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1376 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1377 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1379 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1382 f4a881ce 2018-11-17 stsp static const struct got_error *
1383 f4a881ce 2018-11-17 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1384 f4a881ce 2018-11-17 stsp struct got_object *obj, int fd)
1386 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1387 f4a881ce 2018-11-17 stsp struct imsgbuf *ibuf;
1389 f4a881ce 2018-11-17 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1391 f4a881ce 2018-11-17 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1393 f4a881ce 2018-11-17 stsp return err;
1395 f4a881ce 2018-11-17 stsp return got_privsep_recv_tag(tag, ibuf);
1398 f4a881ce 2018-11-17 stsp const struct got_error *
1399 f4a881ce 2018-11-17 stsp got_object_read_packed_tag_privsep(struct got_tag_object **tag,
1400 f4a881ce 2018-11-17 stsp struct got_object *obj, struct got_pack *pack)
1402 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1404 f4a881ce 2018-11-17 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1406 f4a881ce 2018-11-17 stsp return err;
1408 f4a881ce 2018-11-17 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1411 f4a881ce 2018-11-17 stsp const struct got_error *
1412 f4a881ce 2018-11-17 stsp got_object_read_tag_privsep(struct got_tag_object **tag,
1413 f4a881ce 2018-11-17 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1415 f4a881ce 2018-11-17 stsp int imsg_fds[2];
1416 f4a881ce 2018-11-17 stsp pid_t pid;
1417 f4a881ce 2018-11-17 stsp struct imsgbuf *ibuf;
1419 f4a881ce 2018-11-17 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1420 f4a881ce 2018-11-17 stsp return request_tag(tag, repo, obj, obj_fd);
1422 f4a881ce 2018-11-17 stsp ibuf = calloc(1, sizeof(*ibuf));
1423 f4a881ce 2018-11-17 stsp if (ibuf == NULL)
1424 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1426 f4a881ce 2018-11-17 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1427 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1429 f4a881ce 2018-11-17 stsp pid = fork();
1430 f4a881ce 2018-11-17 stsp if (pid == -1)
1431 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1432 f4a881ce 2018-11-17 stsp else if (pid == 0) {
1433 f4a881ce 2018-11-17 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1434 f4a881ce 2018-11-17 stsp repo->path);
1435 f4a881ce 2018-11-17 stsp /* not reached */
1438 f4a881ce 2018-11-17 stsp close(imsg_fds[1]);
1439 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1440 f4a881ce 2018-11-17 stsp imsg_fds[0];
1441 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1442 f4a881ce 2018-11-17 stsp imsg_init(ibuf, imsg_fds[0]);
1443 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1445 f4a881ce 2018-11-17 stsp return request_tag(tag, repo, obj, obj_fd);