Blame


1 d71d75ad 2017-11-05 stsp /*
2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 d71d75ad 2017-11-05 stsp *
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.
7 d71d75ad 2017-11-05 stsp *
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.
15 d71d75ad 2017-11-05 stsp */
16 d71d75ad 2017-11-05 stsp
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>
24 d1cda826 2017-11-06 stsp
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>
37 d71d75ad 2017-11-05 stsp
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"
42 d71d75ad 2017-11-05 stsp
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"
54 1411938b 2018-02-12 stsp
55 ab9a70b2 2017-11-06 stsp #ifndef MIN
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 ab9a70b2 2017-11-06 stsp #endif
58 ef0981d5 2018-02-12 stsp
59 a1fd68d8 2018-01-12 stsp int
60 a1fd68d8 2018-01-12 stsp got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
61 a1fd68d8 2018-01-12 stsp {
62 a1fd68d8 2018-01-12 stsp return memcmp(id1->sha1, id2->sha1, SHA1_DIGEST_LENGTH);
63 8bf5b3c9 2018-03-17 stsp }
64 8bf5b3c9 2018-03-17 stsp
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)
67 8bf5b3c9 2018-03-17 stsp {
68 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
69 8bf5b3c9 2018-03-17 stsp
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));
74 8bf5b3c9 2018-03-17 stsp return id2;
75 3235492e 2018-04-01 stsp }
76 3235492e 2018-04-01 stsp
77 3235492e 2018-04-01 stsp struct got_object_id *
78 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
79 3235492e 2018-04-01 stsp {
80 6402fb3c 2018-09-15 stsp return &obj->id;
81 bacc9935 2018-05-20 stsp }
82 bacc9935 2018-05-20 stsp
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)
85 bacc9935 2018-05-20 stsp {
86 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
87 a1fd68d8 2018-01-12 stsp }
88 d71d75ad 2017-11-05 stsp
89 b107e67f 2018-01-19 stsp int
90 b107e67f 2018-01-19 stsp got_object_get_type(struct got_object *obj)
91 a1fd68d8 2018-01-12 stsp {
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;
98 96f5e8b3 2018-01-23 stsp default:
99 96f5e8b3 2018-01-23 stsp abort();
100 96f5e8b3 2018-01-23 stsp break;
101 d71d75ad 2017-11-05 stsp }
102 d71d75ad 2017-11-05 stsp
103 96f5e8b3 2018-01-23 stsp /* not reached */
104 96f5e8b3 2018-01-23 stsp return 0;
105 d71d75ad 2017-11-05 stsp }
106 ab9a70b2 2017-11-06 stsp
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)
109 ab9a70b2 2017-11-06 stsp {
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);
113 e6b1056e 2018-04-22 stsp
114 e6b1056e 2018-04-22 stsp *path = NULL;
115 ab9a70b2 2017-11-06 stsp
116 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
117 0a585a0d 2018-03-17 stsp return got_error_from_errno();
118 ab9a70b2 2017-11-06 stsp
119 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
120 ef0981d5 2018-02-12 stsp if (err)
121 7a132809 2018-07-23 stsp goto done;
122 ab9a70b2 2017-11-06 stsp
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();
126 ab9a70b2 2017-11-06 stsp
127 7a132809 2018-07-23 stsp done:
128 ef0981d5 2018-02-12 stsp free(hex);
129 d1cda826 2017-11-06 stsp free(path_objects);
130 d1cda826 2017-11-06 stsp return err;
131 d1cda826 2017-11-06 stsp }
132 d1cda826 2017-11-06 stsp
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)
135 d1cda826 2017-11-06 stsp {
136 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
137 a1fd68d8 2018-01-12 stsp char *path;
138 6c00b545 2018-01-17 stsp
139 6c00b545 2018-01-17 stsp err = object_path(&path, &obj->id, repo);
140 d1cda826 2017-11-06 stsp if (err)
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();
145 6c00b545 2018-01-17 stsp goto done;
146 a1fd68d8 2018-01-12 stsp }
147 4558fcd4 2018-01-14 stsp done:
148 4558fcd4 2018-01-14 stsp free(path);
149 2090a03d 2018-09-09 stsp return err;
150 2090a03d 2018-09-09 stsp }
151 2090a03d 2018-09-09 stsp
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)
154 2090a03d 2018-09-09 stsp {
155 2090a03d 2018-09-09 stsp size_t size;
156 2090a03d 2018-09-09 stsp
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);
161 2090a03d 2018-09-09 stsp
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();
165 2090a03d 2018-09-09 stsp
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);
170 2090a03d 2018-09-09 stsp
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);
173 2090a03d 2018-09-09 stsp
174 2090a03d 2018-09-09 stsp return NULL;
175 2090a03d 2018-09-09 stsp }
176 2090a03d 2018-09-09 stsp
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)
180 2090a03d 2018-09-09 stsp {
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;
184 2090a03d 2018-09-09 stsp int idx;
185 2090a03d 2018-09-09 stsp char *path_packfile;
186 2090a03d 2018-09-09 stsp
187 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
188 2090a03d 2018-09-09 stsp if (err)
189 2090a03d 2018-09-09 stsp return err;
190 2090a03d 2018-09-09 stsp
191 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
192 2090a03d 2018-09-09 stsp if (err)
193 2090a03d 2018-09-09 stsp return err;
194 2090a03d 2018-09-09 stsp
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);
198 2090a03d 2018-09-09 stsp if (err)
199 2090a03d 2018-09-09 stsp goto done;
200 2090a03d 2018-09-09 stsp }
201 2090a03d 2018-09-09 stsp
202 876c234b 2018-09-10 stsp err = got_object_packed_read_privsep(obj, repo, pack, packidx, idx, id);
203 2090a03d 2018-09-09 stsp if (err)
204 2090a03d 2018-09-09 stsp goto done;
205 2090a03d 2018-09-09 stsp
206 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
207 2090a03d 2018-09-09 stsp done:
208 2090a03d 2018-09-09 stsp free(path_packfile);
209 4558fcd4 2018-01-14 stsp return err;
210 4558fcd4 2018-01-14 stsp }
211 a1fd68d8 2018-01-12 stsp
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)
215 4558fcd4 2018-01-14 stsp {
216 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
217 6c00b545 2018-01-17 stsp char *path;
218 2178c42e 2018-04-22 stsp int fd;
219 7bb0daa1 2018-06-21 stsp
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;
224 7bb0daa1 2018-06-21 stsp }
225 4558fcd4 2018-01-14 stsp
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);
232 59790a32 2018-09-15 stsp }
233 59790a32 2018-09-15 stsp
234 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
235 4558fcd4 2018-01-14 stsp if (err)
236 4558fcd4 2018-01-14 stsp return err;
237 4558fcd4 2018-01-14 stsp
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);
242 59790a32 2018-09-15 stsp else
243 6c00b545 2018-01-17 stsp err = got_error_from_errno();
244 59790a32 2018-09-15 stsp goto done;
245 6c00b545 2018-01-17 stsp } else {
246 ad242220 2018-09-08 stsp err = got_object_read_header_privsep(obj, repo, fd);
247 6c00b545 2018-01-17 stsp if (err)
248 6c00b545 2018-01-17 stsp goto done;
249 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
250 6c00b545 2018-01-17 stsp }
251 7bb0daa1 2018-06-21 stsp
252 59790a32 2018-09-15 stsp (*obj)->refcnt++;
253 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
254 6c00b545 2018-01-17 stsp done:
255 6c00b545 2018-01-17 stsp free(path);
256 2178c42e 2018-04-22 stsp if (fd != -1)
257 2178c42e 2018-04-22 stsp close(fd);
258 ab9a70b2 2017-11-06 stsp return err;
259 6c00b545 2018-01-17 stsp
260 ab9a70b2 2017-11-06 stsp }
261 ab9a70b2 2017-11-06 stsp
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)
265 6dfa2fd3 2018-02-12 stsp {
266 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
267 6dfa2fd3 2018-02-12 stsp
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);
270 6dfa2fd3 2018-02-12 stsp
271 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
272 434025f3 2018-09-16 stsp }
273 434025f3 2018-09-16 stsp
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)
277 434025f3 2018-09-16 stsp {
278 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
279 434025f3 2018-09-16 stsp
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;
285 434025f3 2018-09-16 stsp }
286 434025f3 2018-09-16 stsp } else
287 434025f3 2018-09-16 stsp *commit = NULL;
288 434025f3 2018-09-16 stsp
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);
291 434025f3 2018-09-16 stsp
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);
298 434025f3 2018-09-16 stsp if (err)
299 434025f3 2018-09-16 stsp return err;
300 434025f3 2018-09-16 stsp }
301 434025f3 2018-09-16 stsp err = got_object_read_packed_commit_privsep(commit, obj, pack);
302 434025f3 2018-09-16 stsp } else {
303 434025f3 2018-09-16 stsp int fd;
304 434025f3 2018-09-16 stsp err = open_loose_object(&fd, obj, repo);
305 434025f3 2018-09-16 stsp if (err)
306 434025f3 2018-09-16 stsp return err;
307 434025f3 2018-09-16 stsp err = got_object_read_commit_privsep(commit, obj, fd, repo);
308 434025f3 2018-09-16 stsp close(fd);
309 434025f3 2018-09-16 stsp }
310 434025f3 2018-09-16 stsp
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);
314 e32baab7 2018-11-05 stsp }
315 e32baab7 2018-11-05 stsp
316 e32baab7 2018-11-05 stsp return err;
317 e32baab7 2018-11-05 stsp }
318 e32baab7 2018-11-05 stsp
319 e32baab7 2018-11-05 stsp const struct got_error *
320 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
321 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
322 e32baab7 2018-11-05 stsp {
323 e32baab7 2018-11-05 stsp const struct got_error *err;
324 e32baab7 2018-11-05 stsp struct got_object *obj;
325 e32baab7 2018-11-05 stsp
326 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
327 e32baab7 2018-11-05 stsp if (*commit != NULL) {
328 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
329 e32baab7 2018-11-05 stsp return NULL;
330 e32baab7 2018-11-05 stsp }
331 e32baab7 2018-11-05 stsp
332 e32baab7 2018-11-05 stsp err = got_object_open(&obj, repo, id);
333 e32baab7 2018-11-05 stsp if (err)
334 e32baab7 2018-11-05 stsp return err;
335 e32baab7 2018-11-05 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
336 e32baab7 2018-11-05 stsp err = got_error(GOT_ERR_OBJ_TYPE);
337 e32baab7 2018-11-05 stsp goto done;
338 7762fe12 2018-11-05 stsp }
339 7762fe12 2018-11-05 stsp
340 e32baab7 2018-11-05 stsp err = open_commit(commit, repo, obj, 0);
341 e32baab7 2018-11-05 stsp done:
342 e32baab7 2018-11-05 stsp got_object_close(obj);
343 7762fe12 2018-11-05 stsp return err;
344 7762fe12 2018-11-05 stsp }
345 7762fe12 2018-11-05 stsp
346 e32baab7 2018-11-05 stsp const struct got_error *
347 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
348 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
349 e32baab7 2018-11-05 stsp {
350 e32baab7 2018-11-05 stsp return open_commit(commit, repo, obj, 1);
351 e32baab7 2018-11-05 stsp }
352 e32baab7 2018-11-05 stsp
353 7762fe12 2018-11-05 stsp static const struct got_error *
354 7762fe12 2018-11-05 stsp open_mini_commit(struct got_commit_object_mini **commit,
355 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
356 7762fe12 2018-11-05 stsp {
357 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
358 7762fe12 2018-11-05 stsp
359 e32baab7 2018-11-05 stsp if (check_cache) {
360 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_mini_commit(repo, &obj->id);
361 e32baab7 2018-11-05 stsp if (*commit != NULL) {
362 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
363 e32baab7 2018-11-05 stsp return NULL;
364 e32baab7 2018-11-05 stsp }
365 e32baab7 2018-11-05 stsp } else
366 e32baab7 2018-11-05 stsp *commit = NULL;
367 7762fe12 2018-11-05 stsp
368 7762fe12 2018-11-05 stsp if (obj->type != GOT_OBJ_TYPE_COMMIT)
369 7762fe12 2018-11-05 stsp return got_error(GOT_ERR_OBJ_TYPE);
370 7762fe12 2018-11-05 stsp
371 7762fe12 2018-11-05 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
372 7762fe12 2018-11-05 stsp struct got_pack *pack;
373 7762fe12 2018-11-05 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
374 7762fe12 2018-11-05 stsp if (pack == NULL) {
375 7762fe12 2018-11-05 stsp err = got_repo_cache_pack(&pack, repo,
376 7762fe12 2018-11-05 stsp obj->path_packfile, NULL);
377 7762fe12 2018-11-05 stsp if (err)
378 7762fe12 2018-11-05 stsp return err;
379 7762fe12 2018-11-05 stsp }
380 7762fe12 2018-11-05 stsp err = got_object_read_packed_mini_commit_privsep(commit, obj,
381 7762fe12 2018-11-05 stsp pack);
382 7762fe12 2018-11-05 stsp } else {
383 7762fe12 2018-11-05 stsp int fd;
384 7762fe12 2018-11-05 stsp err = open_loose_object(&fd, obj, repo);
385 7762fe12 2018-11-05 stsp if (err)
386 7762fe12 2018-11-05 stsp return err;
387 7762fe12 2018-11-05 stsp err = got_object_read_mini_commit_privsep(commit, obj, fd,
388 7762fe12 2018-11-05 stsp repo);
389 7762fe12 2018-11-05 stsp close(fd);
390 434025f3 2018-09-16 stsp }
391 434025f3 2018-09-16 stsp
392 e32baab7 2018-11-05 stsp if (err == NULL) {
393 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
394 e32baab7 2018-11-05 stsp err = got_repo_cache_mini_commit(repo, &obj->id, *commit);
395 e32baab7 2018-11-05 stsp }
396 e32baab7 2018-11-05 stsp
397 434025f3 2018-09-16 stsp return err;
398 bff6ca00 2018-04-23 stsp }
399 bff6ca00 2018-04-23 stsp
400 bff6ca00 2018-04-23 stsp const struct got_error *
401 e32baab7 2018-11-05 stsp got_object_open_as_mini_commit(struct got_commit_object_mini **commit,
402 be6a1b5a 2018-06-11 stsp struct got_repository *repo, struct got_object_id *id)
403 be6a1b5a 2018-06-11 stsp {
404 be6a1b5a 2018-06-11 stsp const struct got_error *err;
405 be6a1b5a 2018-06-11 stsp struct got_object *obj;
406 835e0dbd 2018-06-21 stsp
407 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_mini_commit(repo, id);
408 e8eb494a 2018-09-16 stsp if (*commit != NULL) {
409 e8eb494a 2018-09-16 stsp (*commit)->refcnt++;
410 e8eb494a 2018-09-16 stsp return NULL;
411 e8eb494a 2018-09-16 stsp }
412 be6a1b5a 2018-06-11 stsp
413 be6a1b5a 2018-06-11 stsp err = got_object_open(&obj, repo, id);
414 be6a1b5a 2018-06-11 stsp if (err)
415 be6a1b5a 2018-06-11 stsp return err;
416 be6a1b5a 2018-06-11 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT) {
417 be6a1b5a 2018-06-11 stsp err = got_error(GOT_ERR_OBJ_TYPE);
418 be6a1b5a 2018-06-11 stsp goto done;
419 be6a1b5a 2018-06-11 stsp }
420 be6a1b5a 2018-06-11 stsp
421 e32baab7 2018-11-05 stsp err = open_mini_commit(commit, repo, obj, 0);
422 be6a1b5a 2018-06-11 stsp done:
423 be6a1b5a 2018-06-11 stsp got_object_close(obj);
424 be6a1b5a 2018-06-11 stsp return err;
425 434025f3 2018-09-16 stsp }
426 434025f3 2018-09-16 stsp
427 434025f3 2018-09-16 stsp const struct got_error *
428 e32baab7 2018-11-05 stsp got_object_mini_commit_open(struct got_commit_object_mini **commit,
429 434025f3 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
430 434025f3 2018-09-16 stsp {
431 e32baab7 2018-11-05 stsp return open_mini_commit(commit, repo, obj, 1);
432 be6a1b5a 2018-06-11 stsp }
433 be6a1b5a 2018-06-11 stsp
434 be6a1b5a 2018-06-11 stsp const struct got_error *
435 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
436 dbc6a6b6 2018-07-12 stsp {
437 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
438 dbc6a6b6 2018-07-12 stsp
439 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
440 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
441 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
442 dbc6a6b6 2018-07-12 stsp
443 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
444 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
445 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
446 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
447 dbc6a6b6 2018-07-12 stsp *qid = NULL;
448 dbc6a6b6 2018-07-12 stsp return err;
449 dbc6a6b6 2018-07-12 stsp }
450 dbc6a6b6 2018-07-12 stsp
451 dbc6a6b6 2018-07-12 stsp return NULL;
452 7e212e3d 2018-09-09 stsp }
453 7e212e3d 2018-09-09 stsp
454 71eb0e7f 2018-09-16 stsp static const struct got_error *
455 71eb0e7f 2018-09-16 stsp open_tree(struct got_tree_object **tree,
456 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj, int check_cache)
457 0ffeb3c2 2017-11-26 stsp {
458 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
459 f6be5c30 2018-06-22 stsp
460 71eb0e7f 2018-09-16 stsp if (check_cache) {
461 71eb0e7f 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, &obj->id);
462 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
463 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
464 71eb0e7f 2018-09-16 stsp return NULL;
465 71eb0e7f 2018-09-16 stsp }
466 71eb0e7f 2018-09-16 stsp } else
467 71eb0e7f 2018-09-16 stsp *tree = NULL;
468 0ffeb3c2 2017-11-26 stsp
469 0ffeb3c2 2017-11-26 stsp if (obj->type != GOT_OBJ_TYPE_TREE)
470 0ffeb3c2 2017-11-26 stsp return got_error(GOT_ERR_OBJ_TYPE);
471 0ffeb3c2 2017-11-26 stsp
472 e0ab43e7 2018-03-16 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
473 e7885405 2018-09-10 stsp struct got_pack *pack;
474 e7885405 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
475 e7885405 2018-09-10 stsp if (pack == NULL) {
476 e7885405 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
477 e7885405 2018-09-10 stsp obj->path_packfile, NULL);
478 e7885405 2018-09-10 stsp if (err)
479 e7885405 2018-09-10 stsp return err;
480 e7885405 2018-09-10 stsp }
481 e7885405 2018-09-10 stsp err = got_object_read_packed_tree_privsep(tree, obj, pack);
482 e0ab43e7 2018-03-16 stsp } else {
483 d5003b79 2018-04-22 stsp int fd;
484 d5003b79 2018-04-22 stsp err = open_loose_object(&fd, obj, repo);
485 e0ab43e7 2018-03-16 stsp if (err)
486 e0ab43e7 2018-03-16 stsp return err;
487 ad242220 2018-09-08 stsp err = got_object_read_tree_privsep(tree, obj, fd, repo);
488 d5003b79 2018-04-22 stsp close(fd);
489 776d4d29 2018-06-17 stsp }
490 f6be5c30 2018-06-22 stsp
491 f6be5c30 2018-06-22 stsp if (err == NULL) {
492 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
493 f6be5c30 2018-06-22 stsp err = got_repo_cache_tree(repo, &obj->id, *tree);
494 f6be5c30 2018-06-22 stsp }
495 f6be5c30 2018-06-22 stsp
496 776d4d29 2018-06-17 stsp return err;
497 776d4d29 2018-06-17 stsp }
498 776d4d29 2018-06-17 stsp
499 776d4d29 2018-06-17 stsp const struct got_error *
500 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
501 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
502 776d4d29 2018-06-17 stsp {
503 776d4d29 2018-06-17 stsp const struct got_error *err;
504 776d4d29 2018-06-17 stsp struct got_object *obj;
505 835e0dbd 2018-06-21 stsp
506 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
507 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
508 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
509 e8eb494a 2018-09-16 stsp return NULL;
510 e8eb494a 2018-09-16 stsp }
511 776d4d29 2018-06-17 stsp
512 776d4d29 2018-06-17 stsp err = got_object_open(&obj, repo, id);
513 776d4d29 2018-06-17 stsp if (err)
514 776d4d29 2018-06-17 stsp return err;
515 1cbc02b6 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE) {
516 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_OBJ_TYPE);
517 776d4d29 2018-06-17 stsp goto done;
518 e0ab43e7 2018-03-16 stsp }
519 776d4d29 2018-06-17 stsp
520 71eb0e7f 2018-09-16 stsp err = open_tree(tree, repo, obj, 0);
521 776d4d29 2018-06-17 stsp done:
522 776d4d29 2018-06-17 stsp got_object_close(obj);
523 0ffeb3c2 2017-11-26 stsp return err;
524 57efb1af 2018-04-24 stsp }
525 ff6b18f8 2018-04-24 stsp
526 71eb0e7f 2018-09-16 stsp const struct got_error *
527 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
528 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
529 71eb0e7f 2018-09-16 stsp {
530 71eb0e7f 2018-09-16 stsp return open_tree(tree, repo, obj, 1);
531 71eb0e7f 2018-09-16 stsp }
532 71eb0e7f 2018-09-16 stsp
533 883f0469 2018-06-23 stsp const struct got_tree_entries *
534 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
535 883f0469 2018-06-23 stsp {
536 883f0469 2018-06-23 stsp return &tree->entries;
537 883f0469 2018-06-23 stsp }
538 3840f4c9 2018-09-12 stsp
539 3840f4c9 2018-09-12 stsp static const struct got_error *
540 3840f4c9 2018-09-12 stsp read_packed_blob_privsep(size_t *size, int outfd, struct got_object *obj,
541 3840f4c9 2018-09-12 stsp struct got_pack *pack)
542 3840f4c9 2018-09-12 stsp {
543 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
544 3840f4c9 2018-09-12 stsp int outfd_child;
545 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
546 24140570 2018-09-09 stsp
547 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
548 3840f4c9 2018-09-12 stsp if (basefd == -1)
549 3840f4c9 2018-09-12 stsp return got_error_from_errno();
550 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
551 3840f4c9 2018-09-12 stsp if (accumfd == -1)
552 3840f4c9 2018-09-12 stsp return got_error_from_errno();
553 3840f4c9 2018-09-12 stsp
554 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
555 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
556 3840f4c9 2018-09-12 stsp return got_error_from_errno();
557 3840f4c9 2018-09-12 stsp
558 3840f4c9 2018-09-12 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
559 3840f4c9 2018-09-12 stsp if (err)
560 3840f4c9 2018-09-12 stsp return err;
561 3840f4c9 2018-09-12 stsp
562 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
563 3840f4c9 2018-09-12 stsp outfd_child);
564 3840f4c9 2018-09-12 stsp if (err) {
565 3840f4c9 2018-09-12 stsp close(outfd_child);
566 3840f4c9 2018-09-12 stsp return err;
567 3840f4c9 2018-09-12 stsp }
568 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
569 3840f4c9 2018-09-12 stsp basefd);
570 3840f4c9 2018-09-12 stsp if (err) {
571 3840f4c9 2018-09-12 stsp close(basefd);
572 3840f4c9 2018-09-12 stsp close(accumfd);
573 3840f4c9 2018-09-12 stsp close(outfd_child);
574 3840f4c9 2018-09-12 stsp return err;
575 3840f4c9 2018-09-12 stsp }
576 3840f4c9 2018-09-12 stsp
577 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
578 3840f4c9 2018-09-12 stsp accumfd);
579 3840f4c9 2018-09-12 stsp if (err) {
580 3840f4c9 2018-09-12 stsp close(accumfd);
581 3840f4c9 2018-09-12 stsp close(outfd_child);
582 3840f4c9 2018-09-12 stsp return err;
583 3840f4c9 2018-09-12 stsp }
584 3840f4c9 2018-09-12 stsp
585 3840f4c9 2018-09-12 stsp err = got_privsep_recv_blob(size, pack->privsep_child->ibuf);
586 3840f4c9 2018-09-12 stsp if (err)
587 3840f4c9 2018-09-12 stsp return err;
588 3840f4c9 2018-09-12 stsp
589 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
590 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
591 3840f4c9 2018-09-12 stsp
592 3840f4c9 2018-09-12 stsp return err;
593 3840f4c9 2018-09-12 stsp }
594 3840f4c9 2018-09-12 stsp
595 68482ea3 2017-11-27 stsp const struct got_error *
596 68482ea3 2017-11-27 stsp got_object_blob_open(struct got_blob_object **blob,
597 68482ea3 2017-11-27 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
598 68482ea3 2017-11-27 stsp {
599 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
600 55da3778 2018-09-10 stsp int outfd;
601 55da3778 2018-09-10 stsp size_t size;
602 55da3778 2018-09-10 stsp struct stat sb;
603 68482ea3 2017-11-27 stsp
604 68482ea3 2017-11-27 stsp if (obj->type != GOT_OBJ_TYPE_BLOB)
605 68482ea3 2017-11-27 stsp return got_error(GOT_ERR_OBJ_TYPE);
606 68482ea3 2017-11-27 stsp
607 7d283eee 2017-11-29 stsp if (blocksize < obj->hdrlen)
608 7d283eee 2017-11-29 stsp return got_error(GOT_ERR_NO_SPACE);
609 7d283eee 2017-11-29 stsp
610 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
611 4558fcd4 2018-01-14 stsp if (*blob == NULL)
612 0a585a0d 2018-03-17 stsp return got_error_from_errno();
613 68482ea3 2017-11-27 stsp
614 55da3778 2018-09-10 stsp outfd = got_opentempfd();
615 55da3778 2018-09-10 stsp if (outfd == -1)
616 55da3778 2018-09-10 stsp return got_error_from_errno();
617 55da3778 2018-09-10 stsp
618 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
619 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
620 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
621 c7254d79 2018-04-24 stsp goto done;
622 15c8b0e6 2018-04-24 stsp }
623 eb651edf 2018-02-11 stsp if (obj->flags & GOT_OBJ_FLAG_PACKED) {
624 55da3778 2018-09-10 stsp struct got_pack *pack;
625 55da3778 2018-09-10 stsp pack = got_repo_get_cached_pack(repo, obj->path_packfile);
626 55da3778 2018-09-10 stsp if (pack == NULL) {
627 55da3778 2018-09-10 stsp err = got_repo_cache_pack(&pack, repo,
628 55da3778 2018-09-10 stsp obj->path_packfile, NULL);
629 55da3778 2018-09-10 stsp if (err)
630 55da3778 2018-09-10 stsp goto done;
631 55da3778 2018-09-10 stsp }
632 3840f4c9 2018-09-12 stsp err = read_packed_blob_privsep(&size, outfd, obj, pack);
633 c7254d79 2018-04-24 stsp if (err)
634 c7254d79 2018-04-24 stsp goto done;
635 55da3778 2018-09-10 stsp obj->size = size;
636 eb651edf 2018-02-11 stsp } else {
637 55da3778 2018-09-10 stsp int infd;
638 c7254d79 2018-04-24 stsp
639 3aca5731 2018-04-24 stsp err = open_loose_object(&infd, obj, repo);
640 c7254d79 2018-04-24 stsp if (err)
641 c7254d79 2018-04-24 stsp goto done;
642 c7254d79 2018-04-24 stsp
643 ad242220 2018-09-08 stsp err = got_object_read_blob_privsep(&size, outfd, infd, repo);
644 3aca5731 2018-04-24 stsp close(infd);
645 57efb1af 2018-04-24 stsp if (err)
646 c7254d79 2018-04-24 stsp goto done;
647 3aca5731 2018-04-24 stsp
648 2967a784 2018-04-24 stsp if (size != obj->hdrlen + obj->size) {
649 1a6b3ab7 2018-04-24 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
650 2967a784 2018-04-24 stsp goto done;
651 2967a784 2018-04-24 stsp }
652 55da3778 2018-09-10 stsp }
653 2967a784 2018-04-24 stsp
654 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
655 55da3778 2018-09-10 stsp err = got_error_from_errno();
656 55da3778 2018-09-10 stsp goto done;
657 55da3778 2018-09-10 stsp }
658 2967a784 2018-04-24 stsp
659 55da3778 2018-09-10 stsp if (sb.st_size != obj->hdrlen + obj->size) {
660 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
661 55da3778 2018-09-10 stsp goto done;
662 55da3778 2018-09-10 stsp }
663 2967a784 2018-04-24 stsp
664 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
665 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
666 55da3778 2018-09-10 stsp err = got_error_from_errno();
667 55da3778 2018-09-10 stsp close(outfd);
668 55da3778 2018-09-10 stsp goto done;
669 68482ea3 2017-11-27 stsp }
670 68482ea3 2017-11-27 stsp
671 7d283eee 2017-11-29 stsp (*blob)->hdrlen = obj->hdrlen;
672 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
673 f78b0693 2017-11-29 stsp memcpy(&(*blob)->id.sha1, obj->id.sha1, SHA1_DIGEST_LENGTH);
674 7d283eee 2017-11-29 stsp
675 c7254d79 2018-04-24 stsp done:
676 55da3778 2018-09-10 stsp if (err) {
677 55da3778 2018-09-10 stsp if (*blob) {
678 55da3778 2018-09-10 stsp if ((*blob)->f)
679 55da3778 2018-09-10 stsp fclose((*blob)->f);
680 55da3778 2018-09-10 stsp free((*blob)->read_buf);
681 55da3778 2018-09-10 stsp free(*blob);
682 55da3778 2018-09-10 stsp *blob = NULL;
683 55da3778 2018-09-10 stsp } else if (outfd != -1)
684 55da3778 2018-09-10 stsp close(outfd);
685 a19581a2 2018-06-21 stsp }
686 a19581a2 2018-06-21 stsp return err;
687 a19581a2 2018-06-21 stsp }
688 a19581a2 2018-06-21 stsp
689 a19581a2 2018-06-21 stsp const struct got_error *
690 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
691 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
692 a19581a2 2018-06-21 stsp size_t blocksize)
693 a19581a2 2018-06-21 stsp {
694 a19581a2 2018-06-21 stsp const struct got_error *err;
695 a19581a2 2018-06-21 stsp struct got_object *obj;
696 835e0dbd 2018-06-21 stsp
697 835e0dbd 2018-06-21 stsp *blob = NULL;
698 a19581a2 2018-06-21 stsp
699 a19581a2 2018-06-21 stsp err = got_object_open(&obj, repo, id);
700 a19581a2 2018-06-21 stsp if (err)
701 a19581a2 2018-06-21 stsp return err;
702 a19581a2 2018-06-21 stsp if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB) {
703 a19581a2 2018-06-21 stsp err = got_error(GOT_ERR_OBJ_TYPE);
704 a19581a2 2018-06-21 stsp goto done;
705 c7254d79 2018-04-24 stsp }
706 a19581a2 2018-06-21 stsp
707 a19581a2 2018-06-21 stsp err = got_object_blob_open(blob, repo, obj, blocksize);
708 a19581a2 2018-06-21 stsp done:
709 a19581a2 2018-06-21 stsp got_object_close(obj);
710 68482ea3 2017-11-27 stsp return err;
711 0ffeb3c2 2017-11-26 stsp }
712 68482ea3 2017-11-27 stsp
713 68482ea3 2017-11-27 stsp void
714 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
715 68482ea3 2017-11-27 stsp {
716 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
717 68482ea3 2017-11-27 stsp fclose(blob->f);
718 68482ea3 2017-11-27 stsp free(blob);
719 f934cf2c 2018-02-12 stsp }
720 f934cf2c 2018-02-12 stsp
721 f934cf2c 2018-02-12 stsp char *
722 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
723 f934cf2c 2018-02-12 stsp {
724 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
725 f934cf2c 2018-02-12 stsp }
726 f934cf2c 2018-02-12 stsp
727 f934cf2c 2018-02-12 stsp size_t
728 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
729 f934cf2c 2018-02-12 stsp {
730 f934cf2c 2018-02-12 stsp return blob->hdrlen;
731 68482ea3 2017-11-27 stsp }
732 68482ea3 2017-11-27 stsp
733 f934cf2c 2018-02-12 stsp const uint8_t *
734 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
735 f934cf2c 2018-02-12 stsp {
736 f934cf2c 2018-02-12 stsp return blob->read_buf;
737 f934cf2c 2018-02-12 stsp }
738 f934cf2c 2018-02-12 stsp
739 68482ea3 2017-11-27 stsp const struct got_error *
740 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
741 68482ea3 2017-11-27 stsp {
742 eb651edf 2018-02-11 stsp size_t n;
743 eb651edf 2018-02-11 stsp
744 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
745 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
746 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
747 eb651edf 2018-02-11 stsp *outlenp = n;
748 35e9ba5d 2018-06-21 stsp return NULL;
749 35e9ba5d 2018-06-21 stsp }
750 35e9ba5d 2018-06-21 stsp
751 35e9ba5d 2018-06-21 stsp const struct got_error *
752 84451b3e 2018-07-10 stsp got_object_blob_dump_to_file(size_t *total_len, size_t *nlines,
753 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
754 35e9ba5d 2018-06-21 stsp {
755 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
756 35e9ba5d 2018-06-21 stsp size_t len, hdrlen;
757 84451b3e 2018-07-10 stsp const uint8_t *buf;
758 84451b3e 2018-07-10 stsp int i;
759 84451b3e 2018-07-10 stsp
760 84451b3e 2018-07-10 stsp if (total_len)
761 84451b3e 2018-07-10 stsp *total_len = 0;
762 84451b3e 2018-07-10 stsp if (nlines)
763 84451b3e 2018-07-10 stsp *nlines = 0;
764 35e9ba5d 2018-06-21 stsp
765 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
766 35e9ba5d 2018-06-21 stsp do {
767 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
768 35e9ba5d 2018-06-21 stsp if (err)
769 35e9ba5d 2018-06-21 stsp return err;
770 35e9ba5d 2018-06-21 stsp if (len == 0)
771 35e9ba5d 2018-06-21 stsp break;
772 84451b3e 2018-07-10 stsp if (total_len)
773 84451b3e 2018-07-10 stsp *total_len += len;
774 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
775 84451b3e 2018-07-10 stsp if (nlines) {
776 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
777 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
778 84451b3e 2018-07-10 stsp (*nlines)++;
779 84451b3e 2018-07-10 stsp }
780 84451b3e 2018-07-10 stsp }
781 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
782 84451b3e 2018-07-10 stsp fwrite(buf + hdrlen, len - hdrlen, 1, outfile);
783 35e9ba5d 2018-06-21 stsp hdrlen = 0;
784 35e9ba5d 2018-06-21 stsp } while (len != 0);
785 35e9ba5d 2018-06-21 stsp
786 35e9ba5d 2018-06-21 stsp fflush(outfile);
787 35e9ba5d 2018-06-21 stsp rewind(outfile);
788 35e9ba5d 2018-06-21 stsp
789 776d4d29 2018-06-17 stsp return NULL;
790 776d4d29 2018-06-17 stsp }
791 776d4d29 2018-06-17 stsp
792 776d4d29 2018-06-17 stsp static struct got_tree_entry *
793 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
794 776d4d29 2018-06-17 stsp {
795 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
796 776d4d29 2018-06-17 stsp
797 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
798 2673a8da 2018-09-19 stsp if (strncmp(te->name, name, len) == 0 && te->name[len] == '\0')
799 776d4d29 2018-06-17 stsp return te;
800 776d4d29 2018-06-17 stsp }
801 eb651edf 2018-02-11 stsp return NULL;
802 776d4d29 2018-06-17 stsp }
803 776d4d29 2018-06-17 stsp
804 776d4d29 2018-06-17 stsp const struct got_error *
805 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
806 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
807 776d4d29 2018-06-17 stsp {
808 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
809 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
810 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
811 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
812 65a9bbe9 2018-09-15 stsp const char *seg, *s;
813 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
814 776d4d29 2018-06-17 stsp
815 27d434c2 2018-09-15 stsp *id = NULL;
816 776d4d29 2018-06-17 stsp
817 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
818 776d4d29 2018-06-17 stsp if (path[0] != '/')
819 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
820 776d4d29 2018-06-17 stsp
821 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
822 776d4d29 2018-06-17 stsp if (err)
823 776d4d29 2018-06-17 stsp goto done;
824 776d4d29 2018-06-17 stsp
825 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
826 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
827 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
828 27d434c2 2018-09-15 stsp if (*id == NULL)
829 27d434c2 2018-09-15 stsp err = got_error_from_errno();
830 ca008b32 2018-07-22 stsp goto done;
831 776d4d29 2018-06-17 stsp }
832 776d4d29 2018-06-17 stsp
833 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
834 776d4d29 2018-06-17 stsp if (err)
835 776d4d29 2018-06-17 stsp goto done;
836 776d4d29 2018-06-17 stsp
837 65a9bbe9 2018-09-15 stsp s = path;
838 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
839 67606321 2018-06-21 stsp len--;
840 776d4d29 2018-06-17 stsp seg = s;
841 65a9bbe9 2018-09-15 stsp seglen = 0;
842 67606321 2018-06-21 stsp while (len > 0) {
843 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
844 776d4d29 2018-06-17 stsp
845 776d4d29 2018-06-17 stsp if (*s != '/') {
846 776d4d29 2018-06-17 stsp s++;
847 67606321 2018-06-21 stsp len--;
848 65a9bbe9 2018-09-15 stsp seglen++;
849 00530cfb 2018-06-21 stsp if (*s)
850 00530cfb 2018-06-21 stsp continue;
851 776d4d29 2018-06-17 stsp }
852 776d4d29 2018-06-17 stsp
853 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
854 db37e2c0 2018-06-21 stsp if (te == NULL) {
855 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
856 776d4d29 2018-06-17 stsp goto done;
857 776d4d29 2018-06-17 stsp }
858 776d4d29 2018-06-17 stsp
859 67606321 2018-06-21 stsp if (len == 0)
860 67606321 2018-06-21 stsp break;
861 67606321 2018-06-21 stsp
862 776d4d29 2018-06-17 stsp seg = s + 1;
863 65a9bbe9 2018-09-15 stsp seglen = 0;
864 776d4d29 2018-06-17 stsp s++;
865 67606321 2018-06-21 stsp len--;
866 776d4d29 2018-06-17 stsp if (*s) {
867 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
868 db37e2c0 2018-06-21 stsp te->id);
869 db37e2c0 2018-06-21 stsp te = NULL;
870 776d4d29 2018-06-17 stsp if (err)
871 776d4d29 2018-06-17 stsp goto done;
872 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
873 776d4d29 2018-06-17 stsp tree = next_tree;
874 776d4d29 2018-06-17 stsp }
875 776d4d29 2018-06-17 stsp }
876 776d4d29 2018-06-17 stsp
877 27d434c2 2018-09-15 stsp if (te) {
878 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
879 27d434c2 2018-09-15 stsp if (*id == NULL)
880 27d434c2 2018-09-15 stsp return got_error_from_errno();
881 27d434c2 2018-09-15 stsp } else
882 776d4d29 2018-06-17 stsp err = got_error(GOT_ERR_NO_OBJ);
883 776d4d29 2018-06-17 stsp done:
884 776d4d29 2018-06-17 stsp if (commit)
885 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
886 776d4d29 2018-06-17 stsp if (tree)
887 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
888 776d4d29 2018-06-17 stsp return err;
889 68482ea3 2017-11-27 stsp }
890 07862c20 2018-09-15 stsp
891 07862c20 2018-09-15 stsp const struct got_error *
892 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
893 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
894 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
895 07862c20 2018-09-15 stsp {
896 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
897 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
898 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
899 65a9bbe9 2018-09-15 stsp const char *seg, *s;
900 65a9bbe9 2018-09-15 stsp size_t seglen, len = strlen(path);
901 07862c20 2018-09-15 stsp
902 07862c20 2018-09-15 stsp *changed = 0;
903 07862c20 2018-09-15 stsp
904 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
905 07862c20 2018-09-15 stsp if (path[0] != '/')
906 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
907 07862c20 2018-09-15 stsp
908 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
909 07862c20 2018-09-15 stsp if (path[1] == '\0')
910 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
911 07862c20 2018-09-15 stsp
912 07862c20 2018-09-15 stsp tree1 = tree01;
913 07862c20 2018-09-15 stsp tree2 = tree02;
914 65a9bbe9 2018-09-15 stsp s = path;
915 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
916 07862c20 2018-09-15 stsp len--;
917 07862c20 2018-09-15 stsp seg = s;
918 65a9bbe9 2018-09-15 stsp seglen = 0;
919 07862c20 2018-09-15 stsp while (len > 0) {
920 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
921 07862c20 2018-09-15 stsp
922 07862c20 2018-09-15 stsp if (*s != '/') {
923 07862c20 2018-09-15 stsp s++;
924 07862c20 2018-09-15 stsp len--;
925 65a9bbe9 2018-09-15 stsp seglen++;
926 07862c20 2018-09-15 stsp if (*s)
927 07862c20 2018-09-15 stsp continue;
928 07862c20 2018-09-15 stsp }
929 07862c20 2018-09-15 stsp
930 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
931 07862c20 2018-09-15 stsp if (te1 == NULL) {
932 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
933 07862c20 2018-09-15 stsp goto done;
934 07862c20 2018-09-15 stsp }
935 07862c20 2018-09-15 stsp
936 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
937 07862c20 2018-09-15 stsp if (te2 == NULL) {
938 07862c20 2018-09-15 stsp *changed = 1;
939 07862c20 2018-09-15 stsp goto done;
940 07862c20 2018-09-15 stsp }
941 07862c20 2018-09-15 stsp
942 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
943 07862c20 2018-09-15 stsp *changed = 1;
944 07862c20 2018-09-15 stsp goto done;
945 07862c20 2018-09-15 stsp }
946 07862c20 2018-09-15 stsp
947 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
948 07862c20 2018-09-15 stsp *changed = 0;
949 07862c20 2018-09-15 stsp goto done;
950 07862c20 2018-09-15 stsp }
951 07862c20 2018-09-15 stsp
952 f970fa8a 2018-09-15 stsp if (len == 0) { /* final path element */
953 07862c20 2018-09-15 stsp *changed = 1;
954 07862c20 2018-09-15 stsp goto done;
955 07862c20 2018-09-15 stsp }
956 07862c20 2018-09-15 stsp
957 07862c20 2018-09-15 stsp seg = s + 1;
958 07862c20 2018-09-15 stsp s++;
959 07862c20 2018-09-15 stsp len--;
960 65a9bbe9 2018-09-15 stsp seglen = 0;
961 07862c20 2018-09-15 stsp if (*s) {
962 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
963 07862c20 2018-09-15 stsp te1->id);
964 07862c20 2018-09-15 stsp te1 = NULL;
965 07862c20 2018-09-15 stsp if (err)
966 07862c20 2018-09-15 stsp goto done;
967 a31cea73 2018-09-15 stsp if (tree1 != tree01)
968 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
969 07862c20 2018-09-15 stsp tree1 = next_tree1;
970 07862c20 2018-09-15 stsp
971 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
972 07862c20 2018-09-15 stsp te2->id);
973 07862c20 2018-09-15 stsp te2 = NULL;
974 07862c20 2018-09-15 stsp if (err)
975 07862c20 2018-09-15 stsp goto done;
976 a31cea73 2018-09-15 stsp if (tree2 != tree02)
977 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
978 07862c20 2018-09-15 stsp tree2 = next_tree2;
979 07862c20 2018-09-15 stsp }
980 07862c20 2018-09-15 stsp }
981 07862c20 2018-09-15 stsp done:
982 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
983 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
984 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
985 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
986 77880158 2018-11-04 stsp return err;
987 77880158 2018-11-04 stsp }
988 77880158 2018-11-04 stsp
989 77880158 2018-11-04 stsp static void
990 77880158 2018-11-04 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
991 77880158 2018-11-04 stsp {
992 77880158 2018-11-04 stsp close(imsg_fds[0]);
993 77880158 2018-11-04 stsp
994 77880158 2018-11-04 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
995 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
996 77880158 2018-11-04 stsp strerror(errno));
997 77880158 2018-11-04 stsp _exit(1);
998 77880158 2018-11-04 stsp }
999 77880158 2018-11-04 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
1000 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s\n", getprogname(),
1001 77880158 2018-11-04 stsp strerror(errno));
1002 77880158 2018-11-04 stsp _exit(1);
1003 77880158 2018-11-04 stsp }
1004 77880158 2018-11-04 stsp
1005 77880158 2018-11-04 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
1006 77880158 2018-11-04 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
1007 77880158 2018-11-04 stsp strerror(errno));
1008 77880158 2018-11-04 stsp _exit(1);
1009 77880158 2018-11-04 stsp }
1010 77880158 2018-11-04 stsp }
1011 77880158 2018-11-04 stsp
1012 77880158 2018-11-04 stsp static const struct got_error *
1013 77880158 2018-11-04 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
1014 77880158 2018-11-04 stsp {
1015 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1016 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1017 77880158 2018-11-04 stsp
1018 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
1019 77880158 2018-11-04 stsp
1020 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, NULL);
1021 77880158 2018-11-04 stsp if (err)
1022 77880158 2018-11-04 stsp return err;
1023 77880158 2018-11-04 stsp
1024 77880158 2018-11-04 stsp return got_privsep_recv_obj(obj, ibuf);
1025 77880158 2018-11-04 stsp }
1026 77880158 2018-11-04 stsp
1027 77880158 2018-11-04 stsp const struct got_error *
1028 77880158 2018-11-04 stsp got_object_read_header_privsep(struct got_object **obj,
1029 77880158 2018-11-04 stsp struct got_repository *repo, int obj_fd)
1030 77880158 2018-11-04 stsp {
1031 77880158 2018-11-04 stsp int imsg_fds[2];
1032 77880158 2018-11-04 stsp pid_t pid;
1033 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1034 77880158 2018-11-04 stsp
1035 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
1036 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1037 77880158 2018-11-04 stsp
1038 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1039 77880158 2018-11-04 stsp if (ibuf == NULL)
1040 77880158 2018-11-04 stsp return got_error_from_errno();
1041 77880158 2018-11-04 stsp
1042 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1043 77880158 2018-11-04 stsp return got_error_from_errno();
1044 77880158 2018-11-04 stsp
1045 77880158 2018-11-04 stsp pid = fork();
1046 77880158 2018-11-04 stsp if (pid == -1)
1047 77880158 2018-11-04 stsp return got_error_from_errno();
1048 77880158 2018-11-04 stsp else if (pid == 0) {
1049 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
1050 77880158 2018-11-04 stsp repo->path);
1051 77880158 2018-11-04 stsp /* not reached */
1052 77880158 2018-11-04 stsp }
1053 77880158 2018-11-04 stsp
1054 77880158 2018-11-04 stsp close(imsg_fds[1]);
1055 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
1056 77880158 2018-11-04 stsp imsg_fds[0];
1057 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
1058 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1059 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
1060 77880158 2018-11-04 stsp
1061 77880158 2018-11-04 stsp return request_object(obj, repo, obj_fd);
1062 77880158 2018-11-04 stsp }
1063 77880158 2018-11-04 stsp
1064 77880158 2018-11-04 stsp static const struct got_error *
1065 77880158 2018-11-04 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
1066 77880158 2018-11-04 stsp struct got_object_id *id)
1067 77880158 2018-11-04 stsp {
1068 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1069 77880158 2018-11-04 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
1070 77880158 2018-11-04 stsp
1071 77880158 2018-11-04 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
1072 77880158 2018-11-04 stsp if (err)
1073 77880158 2018-11-04 stsp return err;
1074 77880158 2018-11-04 stsp
1075 77880158 2018-11-04 stsp err = got_privsep_recv_obj(obj, ibuf);
1076 77880158 2018-11-04 stsp if (err)
1077 77880158 2018-11-04 stsp return err;
1078 77880158 2018-11-04 stsp
1079 77880158 2018-11-04 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
1080 77880158 2018-11-04 stsp if ((*obj)->path_packfile == NULL) {
1081 77880158 2018-11-04 stsp err = got_error_from_errno();
1082 77880158 2018-11-04 stsp return err;
1083 77880158 2018-11-04 stsp }
1084 77880158 2018-11-04 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
1085 77880158 2018-11-04 stsp
1086 77880158 2018-11-04 stsp return NULL;
1087 77880158 2018-11-04 stsp }
1088 77880158 2018-11-04 stsp
1089 77880158 2018-11-04 stsp const struct got_error *
1090 77880158 2018-11-04 stsp got_object_packed_read_privsep(struct got_object **obj,
1091 77880158 2018-11-04 stsp struct got_repository *repo, struct got_pack *pack,
1092 77880158 2018-11-04 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
1093 77880158 2018-11-04 stsp {
1094 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1095 77880158 2018-11-04 stsp int imsg_fds[2];
1096 77880158 2018-11-04 stsp pid_t pid;
1097 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1098 77880158 2018-11-04 stsp
1099 77880158 2018-11-04 stsp if (pack->privsep_child)
1100 77880158 2018-11-04 stsp return request_packed_object(obj, pack, idx, id);
1101 77880158 2018-11-04 stsp
1102 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1103 77880158 2018-11-04 stsp if (ibuf == NULL)
1104 77880158 2018-11-04 stsp return got_error_from_errno();
1105 77880158 2018-11-04 stsp
1106 77880158 2018-11-04 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
1107 77880158 2018-11-04 stsp if (pack->privsep_child == NULL) {
1108 77880158 2018-11-04 stsp err = got_error_from_errno();
1109 77880158 2018-11-04 stsp free(ibuf);
1110 77880158 2018-11-04 stsp return err;
1111 77880158 2018-11-04 stsp }
1112 77880158 2018-11-04 stsp
1113 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1114 77880158 2018-11-04 stsp err = got_error_from_errno();
1115 77880158 2018-11-04 stsp goto done;
1116 77880158 2018-11-04 stsp }
1117 77880158 2018-11-04 stsp
1118 77880158 2018-11-04 stsp pid = fork();
1119 77880158 2018-11-04 stsp if (pid == -1) {
1120 77880158 2018-11-04 stsp err = got_error_from_errno();
1121 77880158 2018-11-04 stsp goto done;
1122 77880158 2018-11-04 stsp } else if (pid == 0) {
1123 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
1124 77880158 2018-11-04 stsp pack->path_packfile);
1125 77880158 2018-11-04 stsp /* not reached */
1126 77880158 2018-11-04 stsp }
1127 77880158 2018-11-04 stsp
1128 77880158 2018-11-04 stsp close(imsg_fds[1]);
1129 77880158 2018-11-04 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
1130 77880158 2018-11-04 stsp pack->privsep_child->pid = pid;
1131 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1132 77880158 2018-11-04 stsp pack->privsep_child->ibuf = ibuf;
1133 77880158 2018-11-04 stsp
1134 77880158 2018-11-04 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
1135 77880158 2018-11-04 stsp if (err) {
1136 77880158 2018-11-04 stsp const struct got_error *child_err;
1137 77880158 2018-11-04 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
1138 77880158 2018-11-04 stsp child_err = got_privsep_wait_for_child(
1139 77880158 2018-11-04 stsp pack->privsep_child->pid);
1140 77880158 2018-11-04 stsp if (child_err && err == NULL)
1141 77880158 2018-11-04 stsp err = child_err;
1142 77880158 2018-11-04 stsp free(ibuf);
1143 77880158 2018-11-04 stsp free(pack->privsep_child);
1144 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1145 77880158 2018-11-04 stsp return err;
1146 77880158 2018-11-04 stsp }
1147 77880158 2018-11-04 stsp
1148 77880158 2018-11-04 stsp done:
1149 77880158 2018-11-04 stsp if (err) {
1150 77880158 2018-11-04 stsp free(ibuf);
1151 77880158 2018-11-04 stsp free(pack->privsep_child);
1152 77880158 2018-11-04 stsp pack->privsep_child = NULL;
1153 77880158 2018-11-04 stsp } else
1154 77880158 2018-11-04 stsp err = request_packed_object(obj, pack, idx, id);
1155 07862c20 2018-09-15 stsp return err;
1156 07862c20 2018-09-15 stsp }
1157 77880158 2018-11-04 stsp
1158 77880158 2018-11-04 stsp static const struct got_error *
1159 77880158 2018-11-04 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
1160 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1161 77880158 2018-11-04 stsp {
1162 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1163 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1164 77880158 2018-11-04 stsp
1165 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1166 77880158 2018-11-04 stsp
1167 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1168 77880158 2018-11-04 stsp if (err)
1169 77880158 2018-11-04 stsp return err;
1170 77880158 2018-11-04 stsp
1171 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, ibuf);
1172 7762fe12 2018-11-05 stsp }
1173 7762fe12 2018-11-05 stsp
1174 7762fe12 2018-11-05 stsp static const struct got_error *
1175 7762fe12 2018-11-05 stsp request_mini_commit(struct got_commit_object_mini **commit,
1176 7762fe12 2018-11-05 stsp struct got_repository *repo, struct got_object *obj, int fd)
1177 7762fe12 2018-11-05 stsp {
1178 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
1179 7762fe12 2018-11-05 stsp struct imsgbuf *ibuf;
1180 7762fe12 2018-11-05 stsp
1181 7762fe12 2018-11-05 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
1182 7762fe12 2018-11-05 stsp
1183 7762fe12 2018-11-05 stsp err = got_privsep_send_mini_commit_req(ibuf, fd, obj);
1184 7762fe12 2018-11-05 stsp if (err)
1185 7762fe12 2018-11-05 stsp return err;
1186 7762fe12 2018-11-05 stsp
1187 7762fe12 2018-11-05 stsp return got_privsep_recv_mini_commit(commit, ibuf);
1188 77880158 2018-11-04 stsp }
1189 77880158 2018-11-04 stsp
1190 77880158 2018-11-04 stsp const struct got_error *
1191 77880158 2018-11-04 stsp got_object_read_packed_commit_privsep(struct got_commit_object **commit,
1192 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1193 77880158 2018-11-04 stsp {
1194 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1195 77880158 2018-11-04 stsp
1196 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1197 77880158 2018-11-04 stsp if (err)
1198 77880158 2018-11-04 stsp return err;
1199 77880158 2018-11-04 stsp
1200 77880158 2018-11-04 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
1201 77880158 2018-11-04 stsp }
1202 77880158 2018-11-04 stsp
1203 77880158 2018-11-04 stsp const struct got_error *
1204 7762fe12 2018-11-05 stsp got_object_read_packed_mini_commit_privsep(struct got_commit_object_mini **commit,
1205 7762fe12 2018-11-05 stsp struct got_object *obj, struct got_pack *pack)
1206 7762fe12 2018-11-05 stsp {
1207 7762fe12 2018-11-05 stsp const struct got_error *err = NULL;
1208 7762fe12 2018-11-05 stsp
1209 7762fe12 2018-11-05 stsp err = got_privsep_send_mini_commit_req(pack->privsep_child->ibuf, -1,
1210 7762fe12 2018-11-05 stsp obj);
1211 7762fe12 2018-11-05 stsp if (err)
1212 7762fe12 2018-11-05 stsp return err;
1213 7762fe12 2018-11-05 stsp
1214 7762fe12 2018-11-05 stsp return got_privsep_recv_mini_commit(commit, pack->privsep_child->ibuf);
1215 7762fe12 2018-11-05 stsp }
1216 7762fe12 2018-11-05 stsp
1217 7762fe12 2018-11-05 stsp const struct got_error *
1218 77880158 2018-11-04 stsp got_object_read_commit_privsep(struct got_commit_object **commit,
1219 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1220 77880158 2018-11-04 stsp {
1221 77880158 2018-11-04 stsp int imsg_fds[2];
1222 77880158 2018-11-04 stsp pid_t pid;
1223 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1224 77880158 2018-11-04 stsp
1225 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1226 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1227 77880158 2018-11-04 stsp
1228 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1229 77880158 2018-11-04 stsp if (ibuf == NULL)
1230 77880158 2018-11-04 stsp return got_error_from_errno();
1231 77880158 2018-11-04 stsp
1232 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1233 77880158 2018-11-04 stsp return got_error_from_errno();
1234 77880158 2018-11-04 stsp
1235 77880158 2018-11-04 stsp pid = fork();
1236 77880158 2018-11-04 stsp if (pid == -1)
1237 77880158 2018-11-04 stsp return got_error_from_errno();
1238 77880158 2018-11-04 stsp else if (pid == 0) {
1239 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1240 77880158 2018-11-04 stsp repo->path);
1241 77880158 2018-11-04 stsp /* not reached */
1242 77880158 2018-11-04 stsp }
1243 77880158 2018-11-04 stsp
1244 77880158 2018-11-04 stsp close(imsg_fds[1]);
1245 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1246 77880158 2018-11-04 stsp imsg_fds[0];
1247 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1248 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1249 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1250 77880158 2018-11-04 stsp
1251 77880158 2018-11-04 stsp return request_commit(commit, repo, obj, obj_fd);
1252 77880158 2018-11-04 stsp }
1253 77880158 2018-11-04 stsp
1254 7762fe12 2018-11-05 stsp const struct got_error *
1255 7762fe12 2018-11-05 stsp got_object_read_mini_commit_privsep(struct got_commit_object_mini **commit,
1256 7762fe12 2018-11-05 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1257 7762fe12 2018-11-05 stsp {
1258 7762fe12 2018-11-05 stsp int imsg_fds[2];
1259 7762fe12 2018-11-05 stsp pid_t pid;
1260 7762fe12 2018-11-05 stsp struct imsgbuf *ibuf;
1261 7762fe12 2018-11-05 stsp
1262 7762fe12 2018-11-05 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
1263 7762fe12 2018-11-05 stsp return request_mini_commit(commit, repo, obj, obj_fd);
1264 7762fe12 2018-11-05 stsp
1265 7762fe12 2018-11-05 stsp ibuf = calloc(1, sizeof(*ibuf));
1266 7762fe12 2018-11-05 stsp if (ibuf == NULL)
1267 7762fe12 2018-11-05 stsp return got_error_from_errno();
1268 7762fe12 2018-11-05 stsp
1269 7762fe12 2018-11-05 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1270 7762fe12 2018-11-05 stsp return got_error_from_errno();
1271 7762fe12 2018-11-05 stsp
1272 7762fe12 2018-11-05 stsp pid = fork();
1273 7762fe12 2018-11-05 stsp if (pid == -1)
1274 7762fe12 2018-11-05 stsp return got_error_from_errno();
1275 7762fe12 2018-11-05 stsp else if (pid == 0) {
1276 7762fe12 2018-11-05 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
1277 7762fe12 2018-11-05 stsp repo->path);
1278 7762fe12 2018-11-05 stsp /* not reached */
1279 7762fe12 2018-11-05 stsp }
1280 7762fe12 2018-11-05 stsp
1281 7762fe12 2018-11-05 stsp close(imsg_fds[1]);
1282 7762fe12 2018-11-05 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
1283 7762fe12 2018-11-05 stsp imsg_fds[0];
1284 7762fe12 2018-11-05 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
1285 7762fe12 2018-11-05 stsp imsg_init(ibuf, imsg_fds[0]);
1286 7762fe12 2018-11-05 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
1287 7762fe12 2018-11-05 stsp
1288 7762fe12 2018-11-05 stsp return request_mini_commit(commit, repo, obj, obj_fd);
1289 7762fe12 2018-11-05 stsp }
1290 7762fe12 2018-11-05 stsp
1291 77880158 2018-11-04 stsp static const struct got_error *
1292 77880158 2018-11-04 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
1293 77880158 2018-11-04 stsp struct got_object *obj, int fd)
1294 77880158 2018-11-04 stsp {
1295 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1296 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1297 77880158 2018-11-04 stsp
1298 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
1299 77880158 2018-11-04 stsp
1300 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(ibuf, fd, obj);
1301 77880158 2018-11-04 stsp if (err)
1302 77880158 2018-11-04 stsp return err;
1303 77880158 2018-11-04 stsp
1304 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, ibuf);
1305 77880158 2018-11-04 stsp }
1306 77880158 2018-11-04 stsp
1307 77880158 2018-11-04 stsp const struct got_error *
1308 77880158 2018-11-04 stsp got_object_read_tree_privsep(struct got_tree_object **tree,
1309 77880158 2018-11-04 stsp struct got_object *obj, int obj_fd, struct got_repository *repo)
1310 77880158 2018-11-04 stsp {
1311 77880158 2018-11-04 stsp int imsg_fds[2];
1312 77880158 2018-11-04 stsp pid_t pid;
1313 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1314 77880158 2018-11-04 stsp
1315 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
1316 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1317 77880158 2018-11-04 stsp
1318 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1319 77880158 2018-11-04 stsp if (ibuf == NULL)
1320 77880158 2018-11-04 stsp return got_error_from_errno();
1321 77880158 2018-11-04 stsp
1322 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1323 77880158 2018-11-04 stsp return got_error_from_errno();
1324 77880158 2018-11-04 stsp
1325 77880158 2018-11-04 stsp pid = fork();
1326 77880158 2018-11-04 stsp if (pid == -1)
1327 77880158 2018-11-04 stsp return got_error_from_errno();
1328 77880158 2018-11-04 stsp else if (pid == 0) {
1329 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
1330 77880158 2018-11-04 stsp repo->path);
1331 77880158 2018-11-04 stsp /* not reached */
1332 77880158 2018-11-04 stsp }
1333 77880158 2018-11-04 stsp
1334 77880158 2018-11-04 stsp close(imsg_fds[1]);
1335 77880158 2018-11-04 stsp
1336 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
1337 77880158 2018-11-04 stsp imsg_fds[0];
1338 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
1339 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1340 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
1341 77880158 2018-11-04 stsp
1342 77880158 2018-11-04 stsp
1343 77880158 2018-11-04 stsp return request_tree(tree, repo, obj, obj_fd);
1344 77880158 2018-11-04 stsp }
1345 77880158 2018-11-04 stsp
1346 77880158 2018-11-04 stsp const struct got_error *
1347 77880158 2018-11-04 stsp got_object_read_packed_tree_privsep(struct got_tree_object **tree,
1348 77880158 2018-11-04 stsp struct got_object *obj, struct got_pack *pack)
1349 77880158 2018-11-04 stsp {
1350 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1351 77880158 2018-11-04 stsp
1352 77880158 2018-11-04 stsp err = got_privsep_send_obj_req(pack->privsep_child->ibuf, -1, obj);
1353 77880158 2018-11-04 stsp if (err)
1354 77880158 2018-11-04 stsp return err;
1355 77880158 2018-11-04 stsp
1356 77880158 2018-11-04 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
1357 77880158 2018-11-04 stsp }
1358 77880158 2018-11-04 stsp
1359 77880158 2018-11-04 stsp static const struct got_error *
1360 77880158 2018-11-04 stsp request_blob(size_t *size, int outfd, int infd, struct imsgbuf *ibuf)
1361 77880158 2018-11-04 stsp {
1362 77880158 2018-11-04 stsp const struct got_error *err = NULL;
1363 77880158 2018-11-04 stsp int outfd_child;
1364 77880158 2018-11-04 stsp
1365 77880158 2018-11-04 stsp outfd_child = dup(outfd);
1366 77880158 2018-11-04 stsp if (outfd_child == -1)
1367 77880158 2018-11-04 stsp return got_error_from_errno();
1368 77880158 2018-11-04 stsp
1369 77880158 2018-11-04 stsp err = got_privsep_send_blob_req(ibuf, infd);
1370 77880158 2018-11-04 stsp if (err)
1371 77880158 2018-11-04 stsp return err;
1372 77880158 2018-11-04 stsp
1373 77880158 2018-11-04 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1374 77880158 2018-11-04 stsp if (err) {
1375 77880158 2018-11-04 stsp close(outfd_child);
1376 77880158 2018-11-04 stsp return err;
1377 77880158 2018-11-04 stsp }
1378 77880158 2018-11-04 stsp
1379 77880158 2018-11-04 stsp err = got_privsep_recv_blob(size, ibuf);
1380 77880158 2018-11-04 stsp if (err)
1381 77880158 2018-11-04 stsp return err;
1382 77880158 2018-11-04 stsp
1383 77880158 2018-11-04 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1384 77880158 2018-11-04 stsp return got_error_from_errno();
1385 77880158 2018-11-04 stsp
1386 77880158 2018-11-04 stsp return err;
1387 77880158 2018-11-04 stsp }
1388 77880158 2018-11-04 stsp
1389 77880158 2018-11-04 stsp const struct got_error *
1390 77880158 2018-11-04 stsp got_object_read_blob_privsep(size_t *size, int outfd, int infd,
1391 77880158 2018-11-04 stsp struct got_repository *repo)
1392 77880158 2018-11-04 stsp {
1393 77880158 2018-11-04 stsp int imsg_fds[2];
1394 77880158 2018-11-04 stsp pid_t pid;
1395 77880158 2018-11-04 stsp struct imsgbuf *ibuf;
1396 77880158 2018-11-04 stsp
1397 77880158 2018-11-04 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1398 77880158 2018-11-04 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1399 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1400 77880158 2018-11-04 stsp }
1401 77880158 2018-11-04 stsp
1402 77880158 2018-11-04 stsp ibuf = calloc(1, sizeof(*ibuf));
1403 77880158 2018-11-04 stsp if (ibuf == NULL)
1404 77880158 2018-11-04 stsp return got_error_from_errno();
1405 77880158 2018-11-04 stsp
1406 77880158 2018-11-04 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1407 77880158 2018-11-04 stsp return got_error_from_errno();
1408 77880158 2018-11-04 stsp
1409 77880158 2018-11-04 stsp pid = fork();
1410 77880158 2018-11-04 stsp if (pid == -1)
1411 77880158 2018-11-04 stsp return got_error_from_errno();
1412 77880158 2018-11-04 stsp else if (pid == 0) {
1413 77880158 2018-11-04 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1414 77880158 2018-11-04 stsp repo->path);
1415 77880158 2018-11-04 stsp /* not reached */
1416 77880158 2018-11-04 stsp }
1417 77880158 2018-11-04 stsp
1418 77880158 2018-11-04 stsp close(imsg_fds[1]);
1419 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1420 77880158 2018-11-04 stsp imsg_fds[0];
1421 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1422 77880158 2018-11-04 stsp imsg_init(ibuf, imsg_fds[0]);
1423 77880158 2018-11-04 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1424 77880158 2018-11-04 stsp
1425 77880158 2018-11-04 stsp return request_blob(size, outfd, infd, ibuf);
1426 77880158 2018-11-04 stsp }