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