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 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
139 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
140 a1fd68d8 2018-01-12 stsp char *path;
142 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
144 d1cda826 2017-11-06 stsp return err;
145 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
146 d5003b79 2018-04-22 stsp if (*fd == -1) {
147 6c00b545 2018-01-17 stsp err = got_error_from_errno();
151 4558fcd4 2018-01-14 stsp free(path);
152 2090a03d 2018-09-09 stsp return err;
155 2090a03d 2018-09-09 stsp static const struct got_error *
156 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
158 2090a03d 2018-09-09 stsp size_t size;
160 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
161 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
162 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
163 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
165 08451938 2018-11-05 stsp *path_packfile = malloc(size);
166 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
167 2090a03d 2018-09-09 stsp return got_error_from_errno();
169 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
170 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
171 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
172 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
174 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
175 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
177 2090a03d 2018-09-09 stsp return NULL;
180 2090a03d 2018-09-09 stsp static const struct got_error *
181 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
182 2090a03d 2018-09-09 stsp struct got_repository *repo)
184 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
185 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
186 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
188 2090a03d 2018-09-09 stsp char *path_packfile;
190 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
192 2090a03d 2018-09-09 stsp return err;
194 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
196 2090a03d 2018-09-09 stsp return err;
198 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
199 2090a03d 2018-09-09 stsp if (pack == NULL) {
200 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
205 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
209 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
211 2090a03d 2018-09-09 stsp free(path_packfile);
212 4558fcd4 2018-01-14 stsp return err;
215 4558fcd4 2018-01-14 stsp const struct got_error *
216 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
217 4558fcd4 2018-01-14 stsp struct got_object_id *id)
219 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
220 6c00b545 2018-01-17 stsp char *path;
223 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
224 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
225 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
226 7bb0daa1 2018-06-21 stsp return NULL;
229 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
230 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
231 59790a32 2018-09-15 stsp return err;
232 59790a32 2018-09-15 stsp if (*obj) {
233 59790a32 2018-09-15 stsp (*obj)->refcnt++;
234 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
237 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
239 4558fcd4 2018-01-14 stsp return err;
241 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
242 2178c42e 2018-04-22 stsp if (fd == -1) {
243 59790a32 2018-09-15 stsp if (errno == ENOENT)
244 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
246 6c00b545 2018-01-17 stsp err = got_error_from_errno();
249 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
252 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
255 59790a32 2018-09-15 stsp (*obj)->refcnt++;
256 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
258 6c00b545 2018-01-17 stsp free(path);
259 2178c42e 2018-04-22 stsp if (fd != -1)
261 ab9a70b2 2017-11-06 stsp return err;
265 6dfa2fd3 2018-02-12 stsp const struct got_error *
266 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
267 6dfa2fd3 2018-02-12 stsp const char *id_str)
269 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
271 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
272 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
274 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
277 15a94983 2018-12-23 stsp const struct got_error *
278 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
279 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
281 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
282 15a94983 2018-12-23 stsp struct got_object *obj;
284 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
286 15a94983 2018-12-23 stsp return err;
288 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
289 15a94983 2018-12-23 stsp got_object_close(obj);
290 15a94983 2018-12-23 stsp if (*id == NULL)
291 15a94983 2018-12-23 stsp return got_error_from_errno();
293 15a94983 2018-12-23 stsp return NULL;
296 434025f3 2018-09-16 stsp static const struct got_error *
297 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
298 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
300 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
302 434025f3 2018-09-16 stsp if (check_cache) {
303 434025f3 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
304 434025f3 2018-09-16 stsp if (*commit != NULL) {
305 434025f3 2018-09-16 stsp (*commit)->refcnt++;
306 434025f3 2018-09-16 stsp return NULL;
309 434025f3 2018-09-16 stsp *commit = NULL;
311 434025f3 2018-09-16 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
312 434025f3 2018-09-16 stsp return got_error(GOT_ERR_OBJ_TYPE);
314 434025f3 2018-09-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
315 434025f3 2018-09-16 stsp struct got_pack *pack;
316 434025f3 2018-09-16 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
317 434025f3 2018-09-16 stsp if (pack == NULL) {
318 434025f3 2018-09-16 stsp err = got_repo_cache_pack(&pack, repo,
319 434025f3 2018-09-16 stsp obj->path_packfile, NULL);
321 434025f3 2018-09-16 stsp return err;
323 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
326 434025f3 2018-09-16 stsp err = open_loose_object(&fd, obj, repo);
328 434025f3 2018-09-16 stsp return err;
329 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
333 434025f3 2018-09-16 stsp if (err == NULL) {
334 434025f3 2018-09-16 stsp (*commit)->refcnt++;
335 434025f3 2018-09-16 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
338 e32baab7 2018-11-05 stsp return err;
341 e32baab7 2018-11-05 stsp const struct got_error *
342 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
343 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
345 e32baab7 2018-11-05 stsp const struct got_error *err;
346 e32baab7 2018-11-05 stsp struct got_object *obj;
348 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
349 e32baab7 2018-11-05 stsp if (*commit != NULL) {
350 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
351 e32baab7 2018-11-05 stsp return NULL;
354 e32baab7 2018-11-05 stsp err = got_object_open(&obj, repo, id);
356 e32baab7 2018-11-05 stsp return err;
357 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
358 e32baab7 2018-11-05 stsp err = got_error(GOT_ERR_OBJ_TYPE);
362 e32baab7 2018-11-05 stsp err = open_commit(commit, repo, obj, 0);
364 e32baab7 2018-11-05 stsp got_object_close(obj);
365 7762fe12 2018-11-05 stsp return err;
368 e32baab7 2018-11-05 stsp const struct got_error *
369 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
370 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
372 e32baab7 2018-11-05 stsp return open_commit(commit, repo, obj, 1);
375 434025f3 2018-09-16 stsp const struct got_error *
376 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
378 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
380 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
381 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
382 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
384 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
385 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
386 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
387 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
388 dbc6a6b6 2018-07-12 stsp *qid = NULL;
389 dbc6a6b6 2018-07-12 stsp return err;
392 dbc6a6b6 2018-07-12 stsp return NULL;
395 71eb0e7f 2018-09-16 stsp static const struct got_error *
396 71eb0e7f 2018-09-16 stsp open_tree(struct got_tree_object **tree,
397 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
399 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
401 71eb0e7f 2018-09-16 stsp if (check_cache) {
402 71eb0e7f 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
403 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
404 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
405 71eb0e7f 2018-09-16 stsp return NULL;
408 71eb0e7f 2018-09-16 stsp *tree = NULL;
410 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
411 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
413 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
414 e7885405 2018-09-10 stsp struct got_pack *pack;
415 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
416 e7885405 2018-09-10 stsp if (pack == NULL) {
417 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
418 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
420 e7885405 2018-09-10 stsp return err;
422 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
425 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
427 e0ab43e7 2018-03-16 stsp return err;
428 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
432 f6be5c30 2018-06-22 stsp if (err == NULL) {
433 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
434 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
437 776d4d29 2018-06-17 stsp return err;
440 776d4d29 2018-06-17 stsp const struct got_error *
441 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
442 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
444 776d4d29 2018-06-17 stsp const struct got_error *err;
445 776d4d29 2018-06-17 stsp struct got_object *obj;
447 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
448 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
449 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
450 e8eb494a 2018-09-16 stsp return NULL;
453 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
455 776d4d29 2018-06-17 stsp return err;
456 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_TREE) {
457 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
461 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
463 776d4d29 2018-06-17 stsp got_object_close(obj);
464 0ffeb3c2 2017-11-26 stsp return err;
467 71eb0e7f 2018-09-16 stsp const struct got_error *
468 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
469 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
471 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
474 883f0469 2018-06-23 stsp const struct got_tree_entries *
475 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
477 883f0469 2018-06-23 stsp return &tree->entries;
480 3840f4c9 2018-09-12 stsp static const struct got_error *
481 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
482 3840f4c9 2018-09-12 stsp struct got_pack *pack)
484 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
485 3840f4c9 2018-09-12 stsp int outfd_child;
486 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
488 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
489 3840f4c9 2018-09-12 stsp if (basefd == -1)
490 3840f4c9 2018-09-12 stsp return got_error_from_errno();
491 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
492 3840f4c9 2018-09-12 stsp if (accumfd == -1)
493 3840f4c9 2018-09-12 stsp return got_error_from_errno();
495 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
496 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
497 3840f4c9 2018-09-12 stsp return got_error_from_errno();
499 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
501 3840f4c9 2018-09-12 stsp return err;
503 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
504 3840f4c9 2018-09-12 stsp outfd_child);
506 3840f4c9 2018-09-12 stsp close(outfd_child);
507 3840f4c9 2018-09-12 stsp return err;
509 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
512 3840f4c9 2018-09-12 stsp close(basefd);
513 3840f4c9 2018-09-12 stsp close(accumfd);
514 3840f4c9 2018-09-12 stsp close(outfd_child);
515 3840f4c9 2018-09-12 stsp return err;
518 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
521 3840f4c9 2018-09-12 stsp close(accumfd);
522 3840f4c9 2018-09-12 stsp close(outfd_child);
523 3840f4c9 2018-09-12 stsp return err;
526 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
528 3840f4c9 2018-09-12 stsp return err;
530 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
531 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
533 3840f4c9 2018-09-12 stsp return err;
536 68482ea3 2017-11-27 stsp const struct got_error *
537 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
538 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
540 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
542 55da3778 2018-09-10 stsp size_t size;
543 55da3778 2018-09-10 stsp struct stat sb;
545 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
546 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
548 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
549 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
551 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
552 4558fcd4 2018-01-14 stsp if (*blob == NULL)
553 0a585a0d 2018-03-17 stsp return got_error_from_errno();
555 55da3778 2018-09-10 stsp outfd = got_opentempfd();
556 55da3778 2018-09-10 stsp if (outfd == -1)
557 55da3778 2018-09-10 stsp return got_error_from_errno();
559 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
560 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
561 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
564 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
565 55da3778 2018-09-10 stsp struct got_pack *pack;
566 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
567 55da3778 2018-09-10 stsp if (pack == NULL) {
568 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
569 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
573 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
576 55da3778 2018-09-10 stsp obj->size = size;
580 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
584 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
585 3aca5731 2018-04-24 stsp close(infd);
589 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
590 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
595 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
596 55da3778 2018-09-10 stsp err = got_error_from_errno();
600 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
601 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
605 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
606 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
607 55da3778 2018-09-10 stsp err = got_error_from_errno();
608 55da3778 2018-09-10 stsp close(outfd);
612 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
613 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
614 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
618 55da3778 2018-09-10 stsp if (*blob) {
619 55da3778 2018-09-10 stsp if ((*blob)->f)
620 55da3778 2018-09-10 stsp fclose((*blob)->f);
621 55da3778 2018-09-10 stsp free((*blob)->read_buf);
622 55da3778 2018-09-10 stsp free(*blob);
623 55da3778 2018-09-10 stsp *blob = NULL;
624 55da3778 2018-09-10 stsp } else if (outfd != -1)
625 55da3778 2018-09-10 stsp close(outfd);
627 a19581a2 2018-06-21 stsp return err;
630 a19581a2 2018-06-21 stsp const struct got_error *
631 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
632 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
633 a19581a2 2018-06-21 stsp size_t blocksize)
635 a19581a2 2018-06-21 stsp const struct got_error *err;
636 a19581a2 2018-06-21 stsp struct got_object *obj;
638 835e0dbd 2018-06-21 stsp *blob = NULL;
640 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
642 a19581a2 2018-06-21 stsp return err;
643 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_BLOB) {
644 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
648 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
650 a19581a2 2018-06-21 stsp got_object_close(obj);
651 68482ea3 2017-11-27 stsp return err;
655 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
657 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
658 68482ea3 2017-11-27 stsp fclose(blob->f);
659 68482ea3 2017-11-27 stsp free(blob);
663 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
665 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
669 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
671 f934cf2c 2018-02-12 stsp return blob->hdrlen;
674 f934cf2c 2018-02-12 stsp const uint8_t *
675 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
677 f934cf2c 2018-02-12 stsp return blob->read_buf;
680 68482ea3 2017-11-27 stsp const struct got_error *
681 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
685 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
686 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
687 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
688 eb651edf 2018-02-11 stsp *outlenp = n;
689 35e9ba5d 2018-06-21 stsp return NULL;
692 35e9ba5d 2018-06-21 stsp const struct got_error *
693 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
694 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
696 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
697 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
698 84451b3e 2018-07-10 stsp const uint8_t *buf;
701 84451b3e 2018-07-10 stsp if (total_len)
702 84451b3e 2018-07-10 stsp *total_len = 0;
703 84451b3e 2018-07-10 stsp if (nlines)
704 84451b3e 2018-07-10 stsp *nlines = 0;
706 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
708 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
710 35e9ba5d 2018-06-21 stsp return err;
711 35e9ba5d 2018-06-21 stsp if (len == 0)
713 84451b3e 2018-07-10 stsp if (total_len)
714 84451b3e 2018-07-10 stsp *total_len += len;
715 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
716 84451b3e 2018-07-10 stsp if (nlines) {
717 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
718 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
719 84451b3e 2018-07-10 stsp (*nlines)++;
722 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
723 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
724 35e9ba5d 2018-06-21 stsp hdrlen = 0;
725 35e9ba5d 2018-06-21 stsp } while (len != 0);
727 35e9ba5d 2018-06-21 stsp fflush(outfile);
728 35e9ba5d 2018-06-21 stsp rewind(outfile);
730 776d4d29 2018-06-17 stsp return NULL;
733 f4a881ce 2018-11-17 stsp static const struct got_error *
734 f4a881ce 2018-11-17 stsp open_tag(struct got_tag_object **tag,
735 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
737 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
739 f4a881ce 2018-11-17 stsp if (check_cache) {
740 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, &obj->id);
741 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
742 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
743 f4a881ce 2018-11-17 stsp return NULL;
746 f4a881ce 2018-11-17 stsp *tag = NULL;
748 f4a881ce 2018-11-17 stsp if (obj->type != GOT_OBJ_TYPE_TAG)
749 f4a881ce 2018-11-17 stsp return got_error(GOT_ERR_OBJ_TYPE);
751 f4a881ce 2018-11-17 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
752 f4a881ce 2018-11-17 stsp struct got_pack *pack;
753 f4a881ce 2018-11-17 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
754 f4a881ce 2018-11-17 stsp if (pack == NULL) {
755 f4a881ce 2018-11-17 stsp err = got_repo_cache_pack(&pack, repo,
756 f4a881ce 2018-11-17 stsp obj->path_packfile, NULL);
758 f4a881ce 2018-11-17 stsp return err;
760 f4a881ce 2018-11-17 stsp err = got_object_read_packed_tag_privsep(tag, obj, pack);
763 f4a881ce 2018-11-17 stsp err = open_loose_object(&fd, obj, repo);
765 f4a881ce 2018-11-17 stsp return err;
766 f4a881ce 2018-11-17 stsp err = got_object_read_tag_privsep(tag, obj, fd, repo);
770 f4a881ce 2018-11-17 stsp if (err == NULL) {
771 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
772 f4a881ce 2018-11-17 stsp err = got_repo_cache_tag(repo, &obj->id, *tag);
775 f4a881ce 2018-11-17 stsp return err;
778 f4a881ce 2018-11-17 stsp const struct got_error *
779 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
780 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
782 f4a881ce 2018-11-17 stsp const struct got_error *err;
783 f4a881ce 2018-11-17 stsp struct got_object *obj;
785 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
786 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
787 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
788 f4a881ce 2018-11-17 stsp return NULL;
791 f4a881ce 2018-11-17 stsp err = got_object_open(&obj, repo, id);
793 f4a881ce 2018-11-17 stsp return err;
794 15a94983 2018-12-23 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT) {
795 f4a881ce 2018-11-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
799 f4a881ce 2018-11-17 stsp err = open_tag(tag, repo, obj, 0);
801 f4a881ce 2018-11-17 stsp got_object_close(obj);
802 f4a881ce 2018-11-17 stsp return err;
805 f4a881ce 2018-11-17 stsp const struct got_error *
806 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
807 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
809 f4a881ce 2018-11-17 stsp return open_tag(tag, repo, obj, 1);
812 776d4d29 2018-06-17 stsp static struct got_tree_entry *
813 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
815 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
817 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
818 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
819 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
820 63da309a 2018-11-07 stsp if (cmp < 0)
822 63da309a 2018-11-07 stsp if (cmp > 0)
824 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
827 eb651edf 2018-02-11 stsp return NULL;
830 776d4d29 2018-06-17 stsp const struct got_error *
831 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
832 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
834 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
835 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
836 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
837 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
838 65a9bbe9 2018-09-15 stsp const char *seg, *s;
839 b7cd37e5 2018-11-18 stsp size_t seglen;
841 27d434c2 2018-09-15 stsp *id = NULL;
843 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
844 776d4d29 2018-06-17 stsp if (path[0] != '/')
845 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
847 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
851 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
852 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
853 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
854 27d434c2 2018-09-15 stsp if (*id == NULL)
855 27d434c2 2018-09-15 stsp err = got_error_from_errno();
859 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
864 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
866 65a9bbe9 2018-09-15 stsp seglen = 0;
867 b7cd37e5 2018-11-18 stsp while (*s) {
868 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
870 776d4d29 2018-06-17 stsp if (*s != '/') {
877 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
878 db37e2c0 2018-06-21 stsp if (te == NULL) {
879 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
883 b7cd37e5 2018-11-18 stsp if (*s == '\0')
886 776d4d29 2018-06-17 stsp seg = s + 1;
887 65a9bbe9 2018-09-15 stsp seglen = 0;
890 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
895 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
896 776d4d29 2018-06-17 stsp tree = next_tree;
901 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
902 27d434c2 2018-09-15 stsp if (*id == NULL)
903 27d434c2 2018-09-15 stsp return got_error_from_errno();
905 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
907 776d4d29 2018-06-17 stsp if (commit)
908 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
910 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
911 776d4d29 2018-06-17 stsp return err;
914 07862c20 2018-09-15 stsp const struct got_error *
915 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
916 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
917 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
919 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
920 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
921 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
922 65a9bbe9 2018-09-15 stsp const char *seg, *s;
923 3b7f9878 2018-11-18 stsp size_t seglen;
925 07862c20 2018-09-15 stsp *changed = 0;
927 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
928 07862c20 2018-09-15 stsp if (path[0] != '/')
929 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
931 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
932 07862c20 2018-09-15 stsp if (path[1] == '\0')
933 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
935 07862c20 2018-09-15 stsp tree1 = tree01;
936 07862c20 2018-09-15 stsp tree2 = tree02;
938 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
940 65a9bbe9 2018-09-15 stsp seglen = 0;
941 3b7f9878 2018-11-18 stsp while (*s) {
942 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
944 07862c20 2018-09-15 stsp if (*s != '/') {
951 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
952 07862c20 2018-09-15 stsp if (te1 == NULL) {
953 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
957 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
958 07862c20 2018-09-15 stsp if (te2 == NULL) {
959 07862c20 2018-09-15 stsp *changed = 1;
963 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
964 07862c20 2018-09-15 stsp *changed = 1;
968 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
969 07862c20 2018-09-15 stsp *changed = 0;
973 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
974 07862c20 2018-09-15 stsp *changed = 1;
978 07862c20 2018-09-15 stsp seg = s + 1;
980 65a9bbe9 2018-09-15 stsp seglen = 0;
982 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
984 07862c20 2018-09-15 stsp te1 = NULL;
987 a31cea73 2018-09-15 stsp if (tree1 != tree01)
988 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
989 07862c20 2018-09-15 stsp tree1 = next_tree1;
991 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
993 07862c20 2018-09-15 stsp te2 = NULL;
996 a31cea73 2018-09-15 stsp if (tree2 != tree02)
997 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
998 07862c20 2018-09-15 stsp tree2 = next_tree2;
1002 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1003 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1004 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1005 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1006 77880158 2018-11-04 stsp return err;
1009 77880158 2018-11-04 stsp static void
1010 77880158 2018-11-04 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
1012 77880158 2018-11-04 stsp close(imsg_fds[0]);
1014 77880158 2018-11-04 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
1015 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
1016 77880158 2018-11-04 stsp strerror(errno));
1019 77880158 2018-11-04 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1020 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
1021 77880158 2018-11-04 stsp strerror(errno));
1025 77880158 2018-11-04 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
1026 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1027 77880158 2018-11-04 stsp strerror(errno));
1032 77880158 2018-11-04 stsp static const struct got_error *
1033 77880158 2018-11-04 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
1035 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1036 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1038 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1040 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
1042 77880158 2018-11-04 stsp return err;
1044 77880158 2018-11-04 stsp return got_privsep_recv_obj(obj, ibuf);
1047 77880158 2018-11-04 stsp const struct got_error *
1048 77880158 2018-11-04 stsp got_object_read_header_privsep(struct got_object **obj,
1049 77880158 2018-11-04 stsp struct got_repository *repo, int obj_fd)
1051 77880158 2018-11-04 stsp int imsg_fds[2];
1052 77880158 2018-11-04 stsp pid_t pid;
1053 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1055 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1056 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1058 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1059 77880158 2018-11-04 stsp if (ibuf == NULL)
1060 77880158 2018-11-04 stsp return got_error_from_errno();
1062 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1063 77880158 2018-11-04 stsp return got_error_from_errno();
1065 77880158 2018-11-04 stsp pid = fork();
1066 77880158 2018-11-04 stsp if (pid == -1)
1067 77880158 2018-11-04 stsp return got_error_from_errno();
1068 77880158 2018-11-04 stsp else if (pid == 0) {
1069 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1070 77880158 2018-11-04 stsp repo->path);
1071 77880158 2018-11-04 stsp /* not reached */
1074 77880158 2018-11-04 stsp close(imsg_fds[1]);
1075 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1076 77880158 2018-11-04 stsp imsg_fds[0];
1077 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1078 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1079 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1081 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1084 77880158 2018-11-04 stsp static const struct got_error *
1085 77880158 2018-11-04 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1086 77880158 2018-11-04 stsp struct got_object_id *id)
1088 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1089 77880158 2018-11-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1091 77880158 2018-11-04 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1093 77880158 2018-11-04 stsp return err;
1095 77880158 2018-11-04 stsp err = got_privsep_recv_obj(obj, ibuf);
1097 77880158 2018-11-04 stsp return err;
1099 77880158 2018-11-04 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1100 77880158 2018-11-04 stsp if ((*obj)->path_packfile == NULL) {
1101 77880158 2018-11-04 stsp err = got_error_from_errno();
1102 77880158 2018-11-04 stsp return err;
1104 77880158 2018-11-04 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1106 77880158 2018-11-04 stsp return NULL;
1109 77880158 2018-11-04 stsp const struct got_error *
1110 77880158 2018-11-04 stsp got_object_packed_read_privsep(struct got_object **obj,
1111 77880158 2018-11-04 stsp struct got_repository *repo, struct got_pack *pack,
1112 77880158 2018-11-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1114 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1115 77880158 2018-11-04 stsp int imsg_fds[2];
1116 77880158 2018-11-04 stsp pid_t pid;
1117 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1119 77880158 2018-11-04 stsp if (pack->privsep_child)
1120 77880158 2018-11-04 stsp return request_packed_object(obj, pack, idx, id);
1122 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1123 77880158 2018-11-04 stsp if (ibuf == NULL)
1124 77880158 2018-11-04 stsp return got_error_from_errno();
1126 77880158 2018-11-04 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1127 77880158 2018-11-04 stsp if (pack->privsep_child == NULL) {
1128 77880158 2018-11-04 stsp err = got_error_from_errno();
1129 77880158 2018-11-04 stsp free(ibuf);
1130 77880158 2018-11-04 stsp return err;
1133 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1134 77880158 2018-11-04 stsp err = got_error_from_errno();
1135 77880158 2018-11-04 stsp goto done;
1138 77880158 2018-11-04 stsp pid = fork();
1139 77880158 2018-11-04 stsp if (pid == -1) {
1140 77880158 2018-11-04 stsp err = got_error_from_errno();
1141 77880158 2018-11-04 stsp goto done;
1142 77880158 2018-11-04 stsp } else if (pid == 0) {
1143 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1144 77880158 2018-11-04 stsp pack->path_packfile);
1145 77880158 2018-11-04 stsp /* not reached */
1148 77880158 2018-11-04 stsp close(imsg_fds[1]);
1149 77880158 2018-11-04 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
1150 77880158 2018-11-04 stsp pack->privsep_child->pid = pid;
1151 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1152 77880158 2018-11-04 stsp pack->privsep_child->ibuf = ibuf;
1154 77880158 2018-11-04 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
1155 77880158 2018-11-04 stsp if (err) {
1156 77880158 2018-11-04 stsp const struct got_error *child_err;
1157 77880158 2018-11-04 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1158 77880158 2018-11-04 stsp child_err = got_privsep_wait_for_child(
1159 77880158 2018-11-04 stsp pack->privsep_child->pid);
1160 77880158 2018-11-04 stsp if (child_err && err == NULL)
1161 77880158 2018-11-04 stsp err = child_err;
1162 77880158 2018-11-04 stsp free(ibuf);
1163 77880158 2018-11-04 stsp free(pack->privsep_child);
1164 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1165 77880158 2018-11-04 stsp return err;
1169 77880158 2018-11-04 stsp if (err) {
1170 77880158 2018-11-04 stsp free(ibuf);
1171 77880158 2018-11-04 stsp free(pack->privsep_child);
1172 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1174 77880158 2018-11-04 stsp err = request_packed_object(obj, pack, idx, id);
1175 07862c20 2018-09-15 stsp return err;
1178 77880158 2018-11-04 stsp static const struct got_error *
1179 77880158 2018-11-04 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
1180 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1182 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1183 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1185 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1187 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1189 77880158 2018-11-04 stsp return err;
1191 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, ibuf);
1194 77880158 2018-11-04 stsp const struct got_error *
1195 77880158 2018-11-04 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1196 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1198 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1200 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1202 77880158 2018-11-04 stsp return err;
1204 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1207 77880158 2018-11-04 stsp const struct got_error *
1208 77880158 2018-11-04 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
1209 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1211 77880158 2018-11-04 stsp int imsg_fds[2];
1212 77880158 2018-11-04 stsp pid_t pid;
1213 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1215 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1216 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1218 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1219 77880158 2018-11-04 stsp if (ibuf == NULL)
1220 77880158 2018-11-04 stsp return got_error_from_errno();
1222 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1223 77880158 2018-11-04 stsp return got_error_from_errno();
1225 77880158 2018-11-04 stsp pid = fork();
1226 77880158 2018-11-04 stsp if (pid == -1)
1227 77880158 2018-11-04 stsp return got_error_from_errno();
1228 77880158 2018-11-04 stsp else if (pid == 0) {
1229 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1230 77880158 2018-11-04 stsp repo->path);
1231 77880158 2018-11-04 stsp /* not reached */
1234 77880158 2018-11-04 stsp close(imsg_fds[1]);
1235 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1236 77880158 2018-11-04 stsp imsg_fds[0];
1237 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1238 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1239 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1241 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1244 77880158 2018-11-04 stsp static const struct got_error *
1245 77880158 2018-11-04 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1246 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1248 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1249 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1251 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1253 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1255 77880158 2018-11-04 stsp return err;
1257 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, ibuf);
1260 77880158 2018-11-04 stsp const struct got_error *
1261 77880158 2018-11-04 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
1262 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1264 77880158 2018-11-04 stsp int imsg_fds[2];
1265 77880158 2018-11-04 stsp pid_t pid;
1266 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1268 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1269 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1271 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1272 77880158 2018-11-04 stsp if (ibuf == NULL)
1273 77880158 2018-11-04 stsp return got_error_from_errno();
1275 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1276 77880158 2018-11-04 stsp return got_error_from_errno();
1278 77880158 2018-11-04 stsp pid = fork();
1279 77880158 2018-11-04 stsp if (pid == -1)
1280 77880158 2018-11-04 stsp return got_error_from_errno();
1281 77880158 2018-11-04 stsp else if (pid == 0) {
1282 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1283 77880158 2018-11-04 stsp repo->path);
1284 77880158 2018-11-04 stsp /* not reached */
1287 77880158 2018-11-04 stsp close(imsg_fds[1]);
1289 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1290 77880158 2018-11-04 stsp imsg_fds[0];
1291 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1292 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1293 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1296 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1299 77880158 2018-11-04 stsp const struct got_error *
1300 77880158 2018-11-04 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1301 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1303 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1305 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1307 77880158 2018-11-04 stsp return err;
1309 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1312 77880158 2018-11-04 stsp static const struct got_error *
1313 77880158 2018-11-04 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1315 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1316 77880158 2018-11-04 stsp int outfd_child;
1318 77880158 2018-11-04 stsp outfd_child = dup(outfd);
1319 77880158 2018-11-04 stsp if (outfd_child == -1)
1320 77880158 2018-11-04 stsp return got_error_from_errno();
1322 77880158 2018-11-04 stsp err = got_privsep_send_blob_req(ibuf, infd);
1324 77880158 2018-11-04 stsp return err;
1326 77880158 2018-11-04 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1327 77880158 2018-11-04 stsp if (err) {
1328 77880158 2018-11-04 stsp close(outfd_child);
1329 77880158 2018-11-04 stsp return err;
1332 77880158 2018-11-04 stsp err = got_privsep_recv_blob(size, ibuf);
1334 77880158 2018-11-04 stsp return err;
1336 77880158 2018-11-04 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1337 77880158 2018-11-04 stsp return got_error_from_errno();
1339 77880158 2018-11-04 stsp return err;
1342 77880158 2018-11-04 stsp const struct got_error *
1343 77880158 2018-11-04 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1344 77880158 2018-11-04 stsp struct got_repository *repo)
1346 77880158 2018-11-04 stsp int imsg_fds[2];
1347 77880158 2018-11-04 stsp pid_t pid;
1348 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1350 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1351 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1352 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1355 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1356 77880158 2018-11-04 stsp if (ibuf == NULL)
1357 77880158 2018-11-04 stsp return got_error_from_errno();
1359 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1360 77880158 2018-11-04 stsp return got_error_from_errno();
1362 77880158 2018-11-04 stsp pid = fork();
1363 77880158 2018-11-04 stsp if (pid == -1)
1364 77880158 2018-11-04 stsp return got_error_from_errno();
1365 77880158 2018-11-04 stsp else if (pid == 0) {
1366 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1367 77880158 2018-11-04 stsp repo->path);
1368 77880158 2018-11-04 stsp /* not reached */
1371 77880158 2018-11-04 stsp close(imsg_fds[1]);
1372 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1373 77880158 2018-11-04 stsp imsg_fds[0];
1374 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1375 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1376 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1378 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1381 f4a881ce 2018-11-17 stsp static const struct got_error *
1382 f4a881ce 2018-11-17 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1383 f4a881ce 2018-11-17 stsp struct got_object *obj, int fd)
1385 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1386 f4a881ce 2018-11-17 stsp struct imsgbuf *ibuf;
1388 f4a881ce 2018-11-17 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1390 f4a881ce 2018-11-17 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1392 f4a881ce 2018-11-17 stsp return err;
1394 f4a881ce 2018-11-17 stsp return got_privsep_recv_tag(tag, ibuf);
1397 f4a881ce 2018-11-17 stsp const struct got_error *
1398 f4a881ce 2018-11-17 stsp got_object_read_packed_tag_privsep(struct got_tag_object **tag,
1399 f4a881ce 2018-11-17 stsp struct got_object *obj, struct got_pack *pack)
1401 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1403 f4a881ce 2018-11-17 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1405 f4a881ce 2018-11-17 stsp return err;
1407 f4a881ce 2018-11-17 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1410 f4a881ce 2018-11-17 stsp const struct got_error *
1411 f4a881ce 2018-11-17 stsp got_object_read_tag_privsep(struct got_tag_object **tag,
1412 f4a881ce 2018-11-17 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1414 f4a881ce 2018-11-17 stsp int imsg_fds[2];
1415 f4a881ce 2018-11-17 stsp pid_t pid;
1416 f4a881ce 2018-11-17 stsp struct imsgbuf *ibuf;
1418 f4a881ce 2018-11-17 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1419 f4a881ce 2018-11-17 stsp return request_tag(tag, repo, obj, obj_fd);
1421 f4a881ce 2018-11-17 stsp ibuf = calloc(1, sizeof(*ibuf));
1422 f4a881ce 2018-11-17 stsp if (ibuf == NULL)
1423 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1425 f4a881ce 2018-11-17 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1426 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1428 f4a881ce 2018-11-17 stsp pid = fork();
1429 f4a881ce 2018-11-17 stsp if (pid == -1)
1430 f4a881ce 2018-11-17 stsp return got_error_from_errno();
1431 f4a881ce 2018-11-17 stsp else if (pid == 0) {
1432 f4a881ce 2018-11-17 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1433 f4a881ce 2018-11-17 stsp repo->path);
1434 f4a881ce 2018-11-17 stsp /* not reached */
1437 f4a881ce 2018-11-17 stsp close(imsg_fds[1]);
1438 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1439 f4a881ce 2018-11-17 stsp imsg_fds[0];
1440 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1441 f4a881ce 2018-11-17 stsp imsg_init(ibuf, imsg_fds[0]);
1442 f4a881ce 2018-11-17 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1444 f4a881ce 2018-11-17 stsp return request_tag(tag, repo, obj, obj_fd);