2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 d71d75ad 2017-11-05 stsp * Permission to use, copy, modify, and distribute this software for any
5 d71d75ad 2017-11-05 stsp * purpose with or without fee is hereby granted, provided that the above
6 d71d75ad 2017-11-05 stsp * copyright notice and this permission notice appear in all copies.
8 d71d75ad 2017-11-05 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 d71d75ad 2017-11-05 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 d71d75ad 2017-11-05 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 d71d75ad 2017-11-05 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 d71d75ad 2017-11-05 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 d71d75ad 2017-11-05 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 d71d75ad 2017-11-05 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 2178c42e 2018-04-22 stsp #include <sys/types.h>
18 0ffeb3c2 2017-11-26 stsp #include <sys/stat.h>
19 d1cda826 2017-11-06 stsp #include <sys/queue.h>
20 2178c42e 2018-04-22 stsp #include <sys/uio.h>
21 2178c42e 2018-04-22 stsp #include <sys/socket.h>
22 2178c42e 2018-04-22 stsp #include <sys/wait.h>
23 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
25 a1fd68d8 2018-01-12 stsp #include <errno.h>
26 2178c42e 2018-04-22 stsp #include <fcntl.h>
27 d71d75ad 2017-11-05 stsp #include <stdio.h>
28 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
29 ab9a70b2 2017-11-06 stsp #include <string.h>
30 2178c42e 2018-04-22 stsp #include <stdint.h>
31 d71d75ad 2017-11-05 stsp #include <sha1.h>
32 ab9a70b2 2017-11-06 stsp #include <zlib.h>
33 ab9a70b2 2017-11-06 stsp #include <ctype.h>
34 ab9a70b2 2017-11-06 stsp #include <limits.h>
35 2178c42e 2018-04-22 stsp #include <imsg.h>
36 788c352e 2018-06-16 stsp #include <time.h>
38 ab9a70b2 2017-11-06 stsp #include "got_error.h"
39 d71d75ad 2017-11-05 stsp #include "got_object.h"
40 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
41 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
44 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
45 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
46 2178c42e 2018-04-22 stsp #include "got_lib_path.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
62 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
65 8bf5b3c9 2018-03-17 stsp struct got_object_id *
66 8bf5b3c9 2018-03-17 stsp got_object_id_dup(struct got_object_id *id1)
68 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
70 8bf5b3c9 2018-03-17 stsp id2 = malloc(sizeof(*id2));
71 8bf5b3c9 2018-03-17 stsp if (id2 == NULL)
72 8bf5b3c9 2018-03-17 stsp return NULL;
73 8bf5b3c9 2018-03-17 stsp memcpy(id2, id1, sizeof(*id2));
77 3235492e 2018-04-01 stsp struct got_object_id *
78 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
80 6402fb3c 2018-09-15 stsp return &obj->id;
83 bacc9935 2018-05-20 stsp const struct got_error *
84 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
86 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
90 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
92 b107e67f 2018-01-19 stsp switch (obj->type) {
93 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
94 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
95 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
96 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
97 b107e67f 2018-01-19 stsp return obj->type;
103 96f5e8b3 2018-01-23 stsp /* not reached */
107 ab9a70b2 2017-11-06 stsp static const struct got_error *
108 4558fcd4 2018-01-14 stsp object_path(char **path, struct got_object_id *id, struct got_repository *repo)
110 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
111 7a132809 2018-07-23 stsp char *hex = NULL;
112 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
114 e6b1056e 2018-04-22 stsp *path = NULL;
116 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
117 0a585a0d 2018-03-17 stsp return got_error_from_errno();
119 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
123 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
124 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
125 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
129 d1cda826 2017-11-06 stsp free(path_objects);
130 d1cda826 2017-11-06 stsp return err;
133 4ee4114f 2018-01-23 stsp static const struct got_error *
134 d5003b79 2018-04-22 stsp open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
136 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
137 a1fd68d8 2018-01-12 stsp char *path;
139 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
141 d1cda826 2017-11-06 stsp return err;
142 d5003b79 2018-04-22 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
143 d5003b79 2018-04-22 stsp if (*fd == -1) {
144 6c00b545 2018-01-17 stsp err = got_error_from_errno();
148 4558fcd4 2018-01-14 stsp free(path);
149 2090a03d 2018-09-09 stsp return err;
152 2090a03d 2018-09-09 stsp static const struct got_error *
153 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
155 2090a03d 2018-09-09 stsp size_t size;
157 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
158 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
159 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
160 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
162 2090a03d 2018-09-09 stsp *path_packfile = calloc(size, sizeof(**path_packfile));
163 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
164 2090a03d 2018-09-09 stsp return got_error_from_errno();
166 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
167 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
168 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
169 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
171 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
172 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
174 2090a03d 2018-09-09 stsp return NULL;
177 2090a03d 2018-09-09 stsp static const struct got_error *
178 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
179 2090a03d 2018-09-09 stsp struct got_repository *repo)
181 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
182 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
183 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
185 2090a03d 2018-09-09 stsp char *path_packfile;
187 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
189 2090a03d 2018-09-09 stsp return err;
191 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
193 2090a03d 2018-09-09 stsp return err;
195 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
196 2090a03d 2018-09-09 stsp if (pack == NULL) {
197 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
202 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
206 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
208 2090a03d 2018-09-09 stsp free(path_packfile);
209 4558fcd4 2018-01-14 stsp return err;
212 4558fcd4 2018-01-14 stsp const struct got_error *
213 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
214 4558fcd4 2018-01-14 stsp struct got_object_id *id)
216 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
217 6c00b545 2018-01-17 stsp char *path;
220 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
221 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
222 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
223 7bb0daa1 2018-06-21 stsp return NULL;
226 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
227 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
228 59790a32 2018-09-15 stsp return err;
229 59790a32 2018-09-15 stsp if (*obj) {
230 59790a32 2018-09-15 stsp (*obj)->refcnt++;
231 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
234 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
236 4558fcd4 2018-01-14 stsp return err;
238 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
239 2178c42e 2018-04-22 stsp if (fd == -1) {
240 59790a32 2018-09-15 stsp if (errno == ENOENT)
241 59790a32 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
243 6c00b545 2018-01-17 stsp err = got_error_from_errno();
246 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
249 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
252 59790a32 2018-09-15 stsp (*obj)->refcnt++;
253 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
255 6c00b545 2018-01-17 stsp free(path);
256 2178c42e 2018-04-22 stsp if (fd != -1)
258 ab9a70b2 2017-11-06 stsp return err;
262 6dfa2fd3 2018-02-12 stsp const struct got_error *
263 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
264 6dfa2fd3 2018-02-12 stsp const char *id_str)
266 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
268 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
269 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
271 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
274 434025f3 2018-09-16 stsp static const struct got_error *
275 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
276 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
278 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
280 434025f3 2018-09-16 stsp if (check_cache) {
281 434025f3 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, &obj->id);
282 434025f3 2018-09-16 stsp if (*commit != NULL) {
283 434025f3 2018-09-16 stsp (*commit)->refcnt++;
284 434025f3 2018-09-16 stsp return NULL;
287 434025f3 2018-09-16 stsp *commit = NULL;
289 434025f3 2018-09-16 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
290 434025f3 2018-09-16 stsp return got_error(GOT_ERR_OBJ_TYPE);
292 434025f3 2018-09-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
293 434025f3 2018-09-16 stsp struct got_pack *pack;
294 434025f3 2018-09-16 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
295 434025f3 2018-09-16 stsp if (pack == NULL) {
296 434025f3 2018-09-16 stsp err = got_repo_cache_pack(&pack, repo,
297 434025f3 2018-09-16 stsp obj->path_packfile, NULL);
299 434025f3 2018-09-16 stsp return err;
301 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
304 434025f3 2018-09-16 stsp err = open_loose_object(&fd, obj, repo);
306 434025f3 2018-09-16 stsp return err;
307 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
311 434025f3 2018-09-16 stsp if (err == NULL) {
312 434025f3 2018-09-16 stsp (*commit)->refcnt++;
313 434025f3 2018-09-16 stsp err = got_repo_cache_commit(repo, &obj->id, *commit);
316 7762fe12 2018-11-05 stsp return err;
319 7762fe12 2018-11-05 stsp static const struct got_error *
320 7762fe12 2018-11-05 stsp open_mini_commit(struct got_commit_object_mini **commit,
321 7762fe12 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
323 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
325 7762fe12 2018-11-05 stsp *commit = NULL;
327 7762fe12 2018-11-05 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
328 7762fe12 2018-11-05 stsp return got_error(GOT_ERR_OBJ_TYPE);
330 7762fe12 2018-11-05 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
331 7762fe12 2018-11-05 stsp struct got_pack *pack;
332 7762fe12 2018-11-05 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
333 7762fe12 2018-11-05 stsp if (pack == NULL) {
334 7762fe12 2018-11-05 stsp err = got_repo_cache_pack(&pack, repo,
335 7762fe12 2018-11-05 stsp obj->path_packfile, NULL);
337 7762fe12 2018-11-05 stsp return err;
339 7762fe12 2018-11-05 stsp err = got_object_read_packed_mini_commit_privsep(commit, obj,
343 7762fe12 2018-11-05 stsp err = open_loose_object(&fd, obj, repo);
345 7762fe12 2018-11-05 stsp return err;
346 7762fe12 2018-11-05 stsp err = got_object_read_mini_commit_privsep(commit, obj, fd,
351 434025f3 2018-09-16 stsp return err;
354 bff6ca00 2018-04-23 stsp const struct got_error *
355 be6a1b5a 2018-06-11 stsp got_object_open_as_commit(struct got_commit_object **commit,
356 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
358 be6a1b5a 2018-06-11 stsp const struct got_error *err;
359 be6a1b5a 2018-06-11 stsp struct got_object *obj;
361 e8eb494a 2018-09-16 stsp *commit = got_repo_get_cached_commit(repo, id);
362 e8eb494a 2018-09-16 stsp if (*commit != NULL) {
363 e8eb494a 2018-09-16 stsp (*commit)->refcnt++;
364 e8eb494a 2018-09-16 stsp return NULL;
367 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
369 be6a1b5a 2018-06-11 stsp return err;
370 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
371 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
375 434025f3 2018-09-16 stsp err = open_commit(commit, repo, obj, 0);
377 be6a1b5a 2018-06-11 stsp got_object_close(obj);
378 be6a1b5a 2018-06-11 stsp return err;
381 434025f3 2018-09-16 stsp const struct got_error *
382 434025f3 2018-09-16 stsp got_object_commit_open(struct got_commit_object **commit,
383 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
385 434025f3 2018-09-16 stsp return open_commit(commit, repo, obj, 1);
388 7762fe12 2018-11-05 stsp const struct got_error *
389 7762fe12 2018-11-05 stsp got_object_open_mini_commit(struct got_commit_object_mini **commit,
390 7762fe12 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
392 7762fe12 2018-11-05 stsp const struct got_error *err;
393 7762fe12 2018-11-05 stsp struct got_object *obj;
395 7762fe12 2018-11-05 stsp err = got_object_open(&obj, repo, id);
397 7762fe12 2018-11-05 stsp return err;
398 7762fe12 2018-11-05 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
399 7762fe12 2018-11-05 stsp err = got_error(GOT_ERR_OBJ_TYPE);
403 7762fe12 2018-11-05 stsp err = open_mini_commit(commit, repo, obj);
405 7762fe12 2018-11-05 stsp got_object_close(obj);
406 7762fe12 2018-11-05 stsp return err;
409 be6a1b5a 2018-06-11 stsp const struct got_error *
410 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
412 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
414 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
415 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
416 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
418 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
419 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
420 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
421 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
422 dbc6a6b6 2018-07-12 stsp *qid = NULL;
423 dbc6a6b6 2018-07-12 stsp return err;
426 dbc6a6b6 2018-07-12 stsp return NULL;
429 71eb0e7f 2018-09-16 stsp static const struct got_error *
430 71eb0e7f 2018-09-16 stsp open_tree(struct got_tree_object **tree,
431 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
433 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
435 71eb0e7f 2018-09-16 stsp if (check_cache) {
436 71eb0e7f 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
437 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
438 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
439 71eb0e7f 2018-09-16 stsp return NULL;
442 71eb0e7f 2018-09-16 stsp *tree = NULL;
444 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
445 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
447 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
448 e7885405 2018-09-10 stsp struct got_pack *pack;
449 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
450 e7885405 2018-09-10 stsp if (pack == NULL) {
451 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
452 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
454 e7885405 2018-09-10 stsp return err;
456 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
459 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
461 e0ab43e7 2018-03-16 stsp return err;
462 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
466 f6be5c30 2018-06-22 stsp if (err == NULL) {
467 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
468 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
471 776d4d29 2018-06-17 stsp return err;
474 776d4d29 2018-06-17 stsp const struct got_error *
475 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
476 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
478 776d4d29 2018-06-17 stsp const struct got_error *err;
479 776d4d29 2018-06-17 stsp struct got_object *obj;
481 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
482 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
483 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
484 e8eb494a 2018-09-16 stsp return NULL;
487 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
489 776d4d29 2018-06-17 stsp return err;
490 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
491 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
495 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
497 776d4d29 2018-06-17 stsp got_object_close(obj);
498 0ffeb3c2 2017-11-26 stsp return err;
501 71eb0e7f 2018-09-16 stsp const struct got_error *
502 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
503 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
505 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
508 883f0469 2018-06-23 stsp const struct got_tree_entries *
509 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
511 883f0469 2018-06-23 stsp return &tree->entries;
514 3840f4c9 2018-09-12 stsp static const struct got_error *
515 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
516 3840f4c9 2018-09-12 stsp struct got_pack *pack)
518 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
519 3840f4c9 2018-09-12 stsp int outfd_child;
520 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
522 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
523 3840f4c9 2018-09-12 stsp if (basefd == -1)
524 3840f4c9 2018-09-12 stsp return got_error_from_errno();
525 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
526 3840f4c9 2018-09-12 stsp if (accumfd == -1)
527 3840f4c9 2018-09-12 stsp return got_error_from_errno();
529 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
530 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
531 3840f4c9 2018-09-12 stsp return got_error_from_errno();
533 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
535 3840f4c9 2018-09-12 stsp return err;
537 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
538 3840f4c9 2018-09-12 stsp outfd_child);
540 3840f4c9 2018-09-12 stsp close(outfd_child);
541 3840f4c9 2018-09-12 stsp return err;
543 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
546 3840f4c9 2018-09-12 stsp close(basefd);
547 3840f4c9 2018-09-12 stsp close(accumfd);
548 3840f4c9 2018-09-12 stsp close(outfd_child);
549 3840f4c9 2018-09-12 stsp return err;
552 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
555 3840f4c9 2018-09-12 stsp close(accumfd);
556 3840f4c9 2018-09-12 stsp close(outfd_child);
557 3840f4c9 2018-09-12 stsp return err;
560 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
562 3840f4c9 2018-09-12 stsp return err;
564 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
565 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
567 3840f4c9 2018-09-12 stsp return err;
570 68482ea3 2017-11-27 stsp const struct got_error *
571 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
572 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
574 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
576 55da3778 2018-09-10 stsp size_t size;
577 55da3778 2018-09-10 stsp struct stat sb;
579 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
580 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
582 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
583 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
585 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
586 4558fcd4 2018-01-14 stsp if (*blob == NULL)
587 0a585a0d 2018-03-17 stsp return got_error_from_errno();
589 55da3778 2018-09-10 stsp outfd = got_opentempfd();
590 55da3778 2018-09-10 stsp if (outfd == -1)
591 55da3778 2018-09-10 stsp return got_error_from_errno();
593 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
594 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
595 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
598 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
599 55da3778 2018-09-10 stsp struct got_pack *pack;
600 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
601 55da3778 2018-09-10 stsp if (pack == NULL) {
602 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
603 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
607 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
610 55da3778 2018-09-10 stsp obj->size = size;
614 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
618 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
619 3aca5731 2018-04-24 stsp close(infd);
623 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
624 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
629 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
630 55da3778 2018-09-10 stsp err = got_error_from_errno();
634 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
635 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
639 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
640 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
641 55da3778 2018-09-10 stsp err = got_error_from_errno();
642 55da3778 2018-09-10 stsp close(outfd);
646 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
647 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
648 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
652 55da3778 2018-09-10 stsp if (*blob) {
653 55da3778 2018-09-10 stsp if ((*blob)->f)
654 55da3778 2018-09-10 stsp fclose((*blob)->f);
655 55da3778 2018-09-10 stsp free((*blob)->read_buf);
656 55da3778 2018-09-10 stsp free(*blob);
657 55da3778 2018-09-10 stsp *blob = NULL;
658 55da3778 2018-09-10 stsp } else if (outfd != -1)
659 55da3778 2018-09-10 stsp close(outfd);
661 a19581a2 2018-06-21 stsp return err;
664 a19581a2 2018-06-21 stsp const struct got_error *
665 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
666 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
667 a19581a2 2018-06-21 stsp size_t blocksize)
669 a19581a2 2018-06-21 stsp const struct got_error *err;
670 a19581a2 2018-06-21 stsp struct got_object *obj;
672 835e0dbd 2018-06-21 stsp *blob = NULL;
674 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
676 a19581a2 2018-06-21 stsp return err;
677 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
678 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
682 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
684 a19581a2 2018-06-21 stsp got_object_close(obj);
685 68482ea3 2017-11-27 stsp return err;
689 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
691 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
692 68482ea3 2017-11-27 stsp fclose(blob->f);
693 68482ea3 2017-11-27 stsp free(blob);
697 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
699 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
703 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
705 f934cf2c 2018-02-12 stsp return blob->hdrlen;
708 f934cf2c 2018-02-12 stsp const uint8_t *
709 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
711 f934cf2c 2018-02-12 stsp return blob->read_buf;
714 68482ea3 2017-11-27 stsp const struct got_error *
715 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
719 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
720 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
721 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
722 eb651edf 2018-02-11 stsp *outlenp = n;
723 35e9ba5d 2018-06-21 stsp return NULL;
726 35e9ba5d 2018-06-21 stsp const struct got_error *
727 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
728 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
730 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
731 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
732 84451b3e 2018-07-10 stsp const uint8_t *buf;
735 84451b3e 2018-07-10 stsp if (total_len)
736 84451b3e 2018-07-10 stsp *total_len = 0;
737 84451b3e 2018-07-10 stsp if (nlines)
738 84451b3e 2018-07-10 stsp *nlines = 0;
740 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
742 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
744 35e9ba5d 2018-06-21 stsp return err;
745 35e9ba5d 2018-06-21 stsp if (len == 0)
747 84451b3e 2018-07-10 stsp if (total_len)
748 84451b3e 2018-07-10 stsp *total_len += len;
749 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
750 84451b3e 2018-07-10 stsp if (nlines) {
751 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
752 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
753 84451b3e 2018-07-10 stsp (*nlines)++;
756 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
757 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
758 35e9ba5d 2018-06-21 stsp hdrlen = 0;
759 35e9ba5d 2018-06-21 stsp } while (len != 0);
761 35e9ba5d 2018-06-21 stsp fflush(outfile);
762 35e9ba5d 2018-06-21 stsp rewind(outfile);
764 776d4d29 2018-06-17 stsp return NULL;
767 776d4d29 2018-06-17 stsp static struct got_tree_entry *
768 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
770 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
772 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
773 2673a8da 2018-09-19 stsp if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
776 eb651edf 2018-02-11 stsp return NULL;
779 776d4d29 2018-06-17 stsp const struct got_error *
780 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
781 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
783 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
784 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
785 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
786 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
787 65a9bbe9 2018-09-15 stsp const char *seg, *s;
788 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
790 27d434c2 2018-09-15 stsp *id = NULL;
792 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
793 776d4d29 2018-06-17 stsp if (path[0] != '/')
794 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
796 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
800 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
801 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
802 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
803 27d434c2 2018-09-15 stsp if (*id == NULL)
804 27d434c2 2018-09-15 stsp err = got_error_from_errno();
808 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
813 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
816 65a9bbe9 2018-09-15 stsp seglen = 0;
817 67606321 2018-06-21 stsp while (len > 0) {
818 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
820 776d4d29 2018-06-17 stsp if (*s != '/') {
828 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
829 db37e2c0 2018-06-21 stsp if (te == NULL) {
830 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
834 67606321 2018-06-21 stsp if (len == 0)
837 776d4d29 2018-06-17 stsp seg = s + 1;
838 65a9bbe9 2018-09-15 stsp seglen = 0;
842 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
847 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
848 776d4d29 2018-06-17 stsp tree = next_tree;
853 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
854 27d434c2 2018-09-15 stsp if (*id == NULL)
855 27d434c2 2018-09-15 stsp return got_error_from_errno();
857 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
859 776d4d29 2018-06-17 stsp if (commit)
860 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
862 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
863 776d4d29 2018-06-17 stsp return err;
866 07862c20 2018-09-15 stsp const struct got_error *
867 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
868 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
869 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
871 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
872 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
873 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
874 65a9bbe9 2018-09-15 stsp const char *seg, *s;
875 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
877 07862c20 2018-09-15 stsp *changed = 0;
879 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
880 07862c20 2018-09-15 stsp if (path[0] != '/')
881 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
883 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
884 07862c20 2018-09-15 stsp if (path[1] == '\0')
885 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
887 07862c20 2018-09-15 stsp tree1 = tree01;
888 07862c20 2018-09-15 stsp tree2 = tree02;
890 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
893 65a9bbe9 2018-09-15 stsp seglen = 0;
894 07862c20 2018-09-15 stsp while (len > 0) {
895 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
897 07862c20 2018-09-15 stsp if (*s != '/') {
905 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
906 07862c20 2018-09-15 stsp if (te1 == NULL) {
907 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
911 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
912 07862c20 2018-09-15 stsp if (te2 == NULL) {
913 07862c20 2018-09-15 stsp *changed = 1;
917 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
918 07862c20 2018-09-15 stsp *changed = 1;
922 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
923 07862c20 2018-09-15 stsp *changed = 0;
927 f970fa8a 2018-09-15 stsp if (len == 0) { /* final path element */
928 07862c20 2018-09-15 stsp *changed = 1;
932 07862c20 2018-09-15 stsp seg = s + 1;
935 65a9bbe9 2018-09-15 stsp seglen = 0;
937 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
939 07862c20 2018-09-15 stsp te1 = NULL;
942 a31cea73 2018-09-15 stsp if (tree1 != tree01)
943 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
944 07862c20 2018-09-15 stsp tree1 = next_tree1;
946 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
948 07862c20 2018-09-15 stsp te2 = NULL;
951 a31cea73 2018-09-15 stsp if (tree2 != tree02)
952 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
953 07862c20 2018-09-15 stsp tree2 = next_tree2;
957 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
958 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
959 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
960 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
961 77880158 2018-11-04 stsp return err;
964 77880158 2018-11-04 stsp static void
965 77880158 2018-11-04 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
967 77880158 2018-11-04 stsp close(imsg_fds[0]);
969 77880158 2018-11-04 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
970 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
971 77880158 2018-11-04 stsp strerror(errno));
974 77880158 2018-11-04 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
975 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
976 77880158 2018-11-04 stsp strerror(errno));
980 77880158 2018-11-04 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
981 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
982 77880158 2018-11-04 stsp strerror(errno));
987 77880158 2018-11-04 stsp static const struct got_error *
988 77880158 2018-11-04 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
990 77880158 2018-11-04 stsp const struct got_error *err = NULL;
991 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
993 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
995 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
997 77880158 2018-11-04 stsp return err;
999 77880158 2018-11-04 stsp return got_privsep_recv_obj(obj, ibuf);
1002 77880158 2018-11-04 stsp const struct got_error *
1003 77880158 2018-11-04 stsp got_object_read_header_privsep(struct got_object **obj,
1004 77880158 2018-11-04 stsp struct got_repository *repo, int obj_fd)
1006 77880158 2018-11-04 stsp int imsg_fds[2];
1007 77880158 2018-11-04 stsp pid_t pid;
1008 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1010 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1011 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1013 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1014 77880158 2018-11-04 stsp if (ibuf == NULL)
1015 77880158 2018-11-04 stsp return got_error_from_errno();
1017 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1018 77880158 2018-11-04 stsp return got_error_from_errno();
1020 77880158 2018-11-04 stsp pid = fork();
1021 77880158 2018-11-04 stsp if (pid == -1)
1022 77880158 2018-11-04 stsp return got_error_from_errno();
1023 77880158 2018-11-04 stsp else if (pid == 0) {
1024 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1025 77880158 2018-11-04 stsp repo->path);
1026 77880158 2018-11-04 stsp /* not reached */
1029 77880158 2018-11-04 stsp close(imsg_fds[1]);
1030 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1031 77880158 2018-11-04 stsp imsg_fds[0];
1032 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1033 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1034 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1036 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1039 77880158 2018-11-04 stsp static const struct got_error *
1040 77880158 2018-11-04 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1041 77880158 2018-11-04 stsp struct got_object_id *id)
1043 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1044 77880158 2018-11-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1046 77880158 2018-11-04 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1048 77880158 2018-11-04 stsp return err;
1050 77880158 2018-11-04 stsp err = got_privsep_recv_obj(obj, ibuf);
1052 77880158 2018-11-04 stsp return err;
1054 77880158 2018-11-04 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1055 77880158 2018-11-04 stsp if ((*obj)->path_packfile == NULL) {
1056 77880158 2018-11-04 stsp err = got_error_from_errno();
1057 77880158 2018-11-04 stsp return err;
1059 77880158 2018-11-04 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1061 77880158 2018-11-04 stsp return NULL;
1064 77880158 2018-11-04 stsp const struct got_error *
1065 77880158 2018-11-04 stsp got_object_packed_read_privsep(struct got_object **obj,
1066 77880158 2018-11-04 stsp struct got_repository *repo, struct got_pack *pack,
1067 77880158 2018-11-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1069 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1070 77880158 2018-11-04 stsp int imsg_fds[2];
1071 77880158 2018-11-04 stsp pid_t pid;
1072 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1074 77880158 2018-11-04 stsp if (pack->privsep_child)
1075 77880158 2018-11-04 stsp return request_packed_object(obj, pack, idx, id);
1077 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1078 77880158 2018-11-04 stsp if (ibuf == NULL)
1079 77880158 2018-11-04 stsp return got_error_from_errno();
1081 77880158 2018-11-04 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1082 77880158 2018-11-04 stsp if (pack->privsep_child == NULL) {
1083 77880158 2018-11-04 stsp err = got_error_from_errno();
1084 77880158 2018-11-04 stsp free(ibuf);
1085 77880158 2018-11-04 stsp return err;
1088 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1089 77880158 2018-11-04 stsp err = got_error_from_errno();
1090 77880158 2018-11-04 stsp goto done;
1093 77880158 2018-11-04 stsp pid = fork();
1094 77880158 2018-11-04 stsp if (pid == -1) {
1095 77880158 2018-11-04 stsp err = got_error_from_errno();
1096 77880158 2018-11-04 stsp goto done;
1097 77880158 2018-11-04 stsp } else if (pid == 0) {
1098 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1099 77880158 2018-11-04 stsp pack->path_packfile);
1100 77880158 2018-11-04 stsp /* not reached */
1103 77880158 2018-11-04 stsp close(imsg_fds[1]);
1104 77880158 2018-11-04 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
1105 77880158 2018-11-04 stsp pack->privsep_child->pid = pid;
1106 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1107 77880158 2018-11-04 stsp pack->privsep_child->ibuf = ibuf;
1109 77880158 2018-11-04 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
1110 77880158 2018-11-04 stsp if (err) {
1111 77880158 2018-11-04 stsp const struct got_error *child_err;
1112 77880158 2018-11-04 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1113 77880158 2018-11-04 stsp child_err = got_privsep_wait_for_child(
1114 77880158 2018-11-04 stsp pack->privsep_child->pid);
1115 77880158 2018-11-04 stsp if (child_err && err == NULL)
1116 77880158 2018-11-04 stsp err = child_err;
1117 77880158 2018-11-04 stsp free(ibuf);
1118 77880158 2018-11-04 stsp free(pack->privsep_child);
1119 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1120 77880158 2018-11-04 stsp return err;
1124 77880158 2018-11-04 stsp if (err) {
1125 77880158 2018-11-04 stsp free(ibuf);
1126 77880158 2018-11-04 stsp free(pack->privsep_child);
1127 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1129 77880158 2018-11-04 stsp err = request_packed_object(obj, pack, idx, id);
1130 07862c20 2018-09-15 stsp return err;
1133 77880158 2018-11-04 stsp static const struct got_error *
1134 77880158 2018-11-04 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
1135 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1137 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1138 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1140 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1142 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1144 77880158 2018-11-04 stsp return err;
1146 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, ibuf);
1149 7762fe12 2018-11-05 stsp static const struct got_error *
1150 7762fe12 2018-11-05 stsp request_mini_commit(struct got_commit_object_mini **commit,
1151 7762fe12 2018-11-05 stsp struct got_repository *repo, struct got_object *obj, int fd)
1153 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
1154 7762fe12 2018-11-05 stsp struct imsgbuf *ibuf;
1156 7762fe12 2018-11-05 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1158 7762fe12 2018-11-05 stsp err = got_privsep_send_mini_commit_req(ibuf, fd, obj);
1160 7762fe12 2018-11-05 stsp return err;
1162 7762fe12 2018-11-05 stsp return got_privsep_recv_mini_commit(commit, ibuf);
1165 77880158 2018-11-04 stsp const struct got_error *
1166 77880158 2018-11-04 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1167 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1169 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1171 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1173 77880158 2018-11-04 stsp return err;
1175 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1178 77880158 2018-11-04 stsp const struct got_error *
1179 7762fe12 2018-11-05 stsp got_object_read_packed_mini_commit_privsep(struct got_commit_object_mini **commit,
1180 7762fe12 2018-11-05 stsp struct got_object *obj, struct got_pack *pack)
1182 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
1184 7762fe12 2018-11-05 stsp err = got_privsep_send_mini_commit_req(pack->privsep_child->ibuf, -1,
1187 7762fe12 2018-11-05 stsp return err;
1189 7762fe12 2018-11-05 stsp return got_privsep_recv_mini_commit(commit, pack->privsep_child->ibuf);
1192 7762fe12 2018-11-05 stsp const struct got_error *
1193 77880158 2018-11-04 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
1194 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1196 77880158 2018-11-04 stsp int imsg_fds[2];
1197 77880158 2018-11-04 stsp pid_t pid;
1198 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1200 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1201 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1203 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1204 77880158 2018-11-04 stsp if (ibuf == NULL)
1205 77880158 2018-11-04 stsp return got_error_from_errno();
1207 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1208 77880158 2018-11-04 stsp return got_error_from_errno();
1210 77880158 2018-11-04 stsp pid = fork();
1211 77880158 2018-11-04 stsp if (pid == -1)
1212 77880158 2018-11-04 stsp return got_error_from_errno();
1213 77880158 2018-11-04 stsp else if (pid == 0) {
1214 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1215 77880158 2018-11-04 stsp repo->path);
1216 77880158 2018-11-04 stsp /* not reached */
1219 77880158 2018-11-04 stsp close(imsg_fds[1]);
1220 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1221 77880158 2018-11-04 stsp imsg_fds[0];
1222 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1223 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1224 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1226 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1229 7762fe12 2018-11-05 stsp const struct got_error *
1230 7762fe12 2018-11-05 stsp got_object_read_mini_commit_privsep(struct got_commit_object_mini **commit,
1231 7762fe12 2018-11-05 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1233 7762fe12 2018-11-05 stsp int imsg_fds[2];
1234 7762fe12 2018-11-05 stsp pid_t pid;
1235 7762fe12 2018-11-05 stsp struct imsgbuf *ibuf;
1237 7762fe12 2018-11-05 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1238 7762fe12 2018-11-05 stsp return request_mini_commit(commit, repo, obj, obj_fd);
1240 7762fe12 2018-11-05 stsp ibuf = calloc(1, sizeof(*ibuf));
1241 7762fe12 2018-11-05 stsp if (ibuf == NULL)
1242 7762fe12 2018-11-05 stsp return got_error_from_errno();
1244 7762fe12 2018-11-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1245 7762fe12 2018-11-05 stsp return got_error_from_errno();
1247 7762fe12 2018-11-05 stsp pid = fork();
1248 7762fe12 2018-11-05 stsp if (pid == -1)
1249 7762fe12 2018-11-05 stsp return got_error_from_errno();
1250 7762fe12 2018-11-05 stsp else if (pid == 0) {
1251 7762fe12 2018-11-05 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1252 7762fe12 2018-11-05 stsp repo->path);
1253 7762fe12 2018-11-05 stsp /* not reached */
1256 7762fe12 2018-11-05 stsp close(imsg_fds[1]);
1257 7762fe12 2018-11-05 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1258 7762fe12 2018-11-05 stsp imsg_fds[0];
1259 7762fe12 2018-11-05 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1260 7762fe12 2018-11-05 stsp imsg_init(ibuf, imsg_fds[0]);
1261 7762fe12 2018-11-05 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1263 7762fe12 2018-11-05 stsp return request_mini_commit(commit, repo, obj, obj_fd);
1266 77880158 2018-11-04 stsp static const struct got_error *
1267 77880158 2018-11-04 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1268 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1270 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1271 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1273 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1275 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1277 77880158 2018-11-04 stsp return err;
1279 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, ibuf);
1282 77880158 2018-11-04 stsp const struct got_error *
1283 77880158 2018-11-04 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
1284 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1286 77880158 2018-11-04 stsp int imsg_fds[2];
1287 77880158 2018-11-04 stsp pid_t pid;
1288 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1290 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1291 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1293 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1294 77880158 2018-11-04 stsp if (ibuf == NULL)
1295 77880158 2018-11-04 stsp return got_error_from_errno();
1297 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1298 77880158 2018-11-04 stsp return got_error_from_errno();
1300 77880158 2018-11-04 stsp pid = fork();
1301 77880158 2018-11-04 stsp if (pid == -1)
1302 77880158 2018-11-04 stsp return got_error_from_errno();
1303 77880158 2018-11-04 stsp else if (pid == 0) {
1304 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1305 77880158 2018-11-04 stsp repo->path);
1306 77880158 2018-11-04 stsp /* not reached */
1309 77880158 2018-11-04 stsp close(imsg_fds[1]);
1311 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1312 77880158 2018-11-04 stsp imsg_fds[0];
1313 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1314 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1315 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1318 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1321 77880158 2018-11-04 stsp const struct got_error *
1322 77880158 2018-11-04 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1323 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1325 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1327 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1329 77880158 2018-11-04 stsp return err;
1331 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1334 77880158 2018-11-04 stsp static const struct got_error *
1335 77880158 2018-11-04 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1337 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1338 77880158 2018-11-04 stsp int outfd_child;
1340 77880158 2018-11-04 stsp outfd_child = dup(outfd);
1341 77880158 2018-11-04 stsp if (outfd_child == -1)
1342 77880158 2018-11-04 stsp return got_error_from_errno();
1344 77880158 2018-11-04 stsp err = got_privsep_send_blob_req(ibuf, infd);
1346 77880158 2018-11-04 stsp return err;
1348 77880158 2018-11-04 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1349 77880158 2018-11-04 stsp if (err) {
1350 77880158 2018-11-04 stsp close(outfd_child);
1351 77880158 2018-11-04 stsp return err;
1354 77880158 2018-11-04 stsp err = got_privsep_recv_blob(size, ibuf);
1356 77880158 2018-11-04 stsp return err;
1358 77880158 2018-11-04 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1359 77880158 2018-11-04 stsp return got_error_from_errno();
1361 77880158 2018-11-04 stsp return err;
1364 77880158 2018-11-04 stsp const struct got_error *
1365 77880158 2018-11-04 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1366 77880158 2018-11-04 stsp struct got_repository *repo)
1368 77880158 2018-11-04 stsp int imsg_fds[2];
1369 77880158 2018-11-04 stsp pid_t pid;
1370 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1372 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1373 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1374 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1377 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1378 77880158 2018-11-04 stsp if (ibuf == NULL)
1379 77880158 2018-11-04 stsp return got_error_from_errno();
1381 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1382 77880158 2018-11-04 stsp return got_error_from_errno();
1384 77880158 2018-11-04 stsp pid = fork();
1385 77880158 2018-11-04 stsp if (pid == -1)
1386 77880158 2018-11-04 stsp return got_error_from_errno();
1387 77880158 2018-11-04 stsp else if (pid == 0) {
1388 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1389 77880158 2018-11-04 stsp repo->path);
1390 77880158 2018-11-04 stsp /* not reached */
1393 77880158 2018-11-04 stsp close(imsg_fds[1]);
1394 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1395 77880158 2018-11-04 stsp imsg_fds[0];
1396 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1397 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1398 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1400 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);