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