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