Blame


1 d71d75ad 2017-11-05 stsp /*
2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 b48e2ddb 2019-05-22 stsp #include <sys/resource.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 81a12da5 2020-09-09 naddy #include <unistd.h>
33 ab9a70b2 2017-11-06 stsp #include <zlib.h>
34 ab9a70b2 2017-11-06 stsp #include <ctype.h>
35 e40622f4 2020-07-23 stsp #include <libgen.h>
36 ab9a70b2 2017-11-06 stsp #include <limits.h>
37 2178c42e 2018-04-22 stsp #include <imsg.h>
38 788c352e 2018-06-16 stsp #include <time.h>
39 d71d75ad 2017-11-05 stsp
40 ab9a70b2 2017-11-06 stsp #include "got_error.h"
41 d71d75ad 2017-11-05 stsp #include "got_object.h"
42 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
43 511a516b 2018-05-19 stsp #include "got_opentemp.h"
44 324d37e7 2019-05-11 stsp #include "got_path.h"
45 d71d75ad 2017-11-05 stsp
46 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
47 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
48 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
49 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
50 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
52 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
53 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
54 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
55 7bb0daa1 2018-06-21 stsp #include "got_lib_repository.h"
56 1411938b 2018-02-12 stsp
57 ab9a70b2 2017-11-06 stsp #ifndef MIN
58 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 ab9a70b2 2017-11-06 stsp #endif
60 3235492e 2018-04-01 stsp
61 3235492e 2018-04-01 stsp struct got_object_id *
62 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
63 3235492e 2018-04-01 stsp {
64 6402fb3c 2018-09-15 stsp return &obj->id;
65 bacc9935 2018-05-20 stsp }
66 bacc9935 2018-05-20 stsp
67 bacc9935 2018-05-20 stsp const struct got_error *
68 bacc9935 2018-05-20 stsp got_object_get_id_str(char **outbuf, struct got_object *obj)
69 bacc9935 2018-05-20 stsp {
70 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
71 a1fd68d8 2018-01-12 stsp }
72 d71d75ad 2017-11-05 stsp
73 15a94983 2018-12-23 stsp const struct got_error *
74 15a94983 2018-12-23 stsp got_object_get_type(int *type, struct got_repository *repo,
75 15a94983 2018-12-23 stsp struct got_object_id *id)
76 a1fd68d8 2018-01-12 stsp {
77 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
78 15a94983 2018-12-23 stsp struct got_object *obj;
79 15a94983 2018-12-23 stsp
80 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
81 15a94983 2018-12-23 stsp if (err)
82 15a94983 2018-12-23 stsp return err;
83 15a94983 2018-12-23 stsp
84 b107e67f 2018-01-19 stsp switch (obj->type) {
85 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_COMMIT:
86 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_TREE:
87 a1fd68d8 2018-01-12 stsp case GOT_OBJ_TYPE_BLOB:
88 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
89 15a94983 2018-12-23 stsp *type = obj->type;
90 15a94983 2018-12-23 stsp break;
91 96f5e8b3 2018-01-23 stsp default:
92 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
93 96f5e8b3 2018-01-23 stsp break;
94 d71d75ad 2017-11-05 stsp }
95 d71d75ad 2017-11-05 stsp
96 15a94983 2018-12-23 stsp got_object_close(obj);
97 15a94983 2018-12-23 stsp return err;
98 d71d75ad 2017-11-05 stsp }
99 ab9a70b2 2017-11-06 stsp
100 90bdb554 2019-04-11 stsp const struct got_error *
101 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
102 90bdb554 2019-04-11 stsp 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 41d2888b 2019-08-11 stsp char *path_objects;
107 e6b1056e 2018-04-22 stsp
108 e6b1056e 2018-04-22 stsp *path = NULL;
109 ab9a70b2 2017-11-06 stsp
110 41d2888b 2019-08-11 stsp path_objects = got_repo_get_path_objects(repo);
111 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
112 638f9024 2019-05-13 stsp return got_error_from_errno("got_repo_get_path_objects");
113 ab9a70b2 2017-11-06 stsp
114 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
115 ef0981d5 2018-02-12 stsp if (err)
116 7a132809 2018-07-23 stsp goto done;
117 ab9a70b2 2017-11-06 stsp
118 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
119 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
120 638f9024 2019-05-13 stsp err = got_error_from_errno("asprintf");
121 ab9a70b2 2017-11-06 stsp
122 7a132809 2018-07-23 stsp done:
123 ef0981d5 2018-02-12 stsp free(hex);
124 d1cda826 2017-11-06 stsp free(path_objects);
125 d1cda826 2017-11-06 stsp return err;
126 d1cda826 2017-11-06 stsp }
127 d1cda826 2017-11-06 stsp
128 762d73f4 2021-04-10 stsp const struct got_error *
129 762d73f4 2021-04-10 stsp got_object_open_loose_fd(int *fd, struct got_object_id *id,
130 4796fb13 2018-12-23 stsp struct got_repository *repo)
131 d1cda826 2017-11-06 stsp {
132 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
133 a1fd68d8 2018-01-12 stsp char *path;
134 6c00b545 2018-01-17 stsp
135 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
136 d1cda826 2017-11-06 stsp if (err)
137 d1cda826 2017-11-06 stsp return err;
138 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
139 d5003b79 2018-04-22 stsp if (*fd == -1) {
140 e82b1d81 2019-07-27 stsp err = got_error_from_errno2("open", path);
141 6c00b545 2018-01-17 stsp goto done;
142 a1fd68d8 2018-01-12 stsp }
143 4558fcd4 2018-01-14 stsp done:
144 4558fcd4 2018-01-14 stsp free(path);
145 2090a03d 2018-09-09 stsp return err;
146 2090a03d 2018-09-09 stsp }
147 2090a03d 2018-09-09 stsp
148 2090a03d 2018-09-09 stsp static const struct got_error *
149 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
150 a158c901 2018-12-23 stsp struct got_object_id *id)
151 a158c901 2018-12-23 stsp {
152 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
153 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
154 a158c901 2018-12-23 stsp
155 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
156 a158c901 2018-12-23 stsp if (err)
157 a158c901 2018-12-23 stsp return err;
158 a158c901 2018-12-23 stsp
159 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
160 a158c901 2018-12-23 stsp if (err)
161 a158c901 2018-12-23 stsp return err;
162 a158c901 2018-12-23 stsp
163 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
164 a158c901 2018-12-23 stsp
165 a158c901 2018-12-23 stsp return NULL;
166 b48e2ddb 2019-05-22 stsp }
167 b48e2ddb 2019-05-22 stsp
168 59d1e4a0 2021-03-10 stsp static const struct got_error *
169 59d1e4a0 2021-03-10 stsp request_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
170 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, int idx, struct got_object_id *id)
171 59d1e4a0 2021-03-10 stsp {
172 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
173 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
174 59d1e4a0 2021-03-10 stsp int outfd_child;
175 59d1e4a0 2021-03-10 stsp int basefd, accumfd; /* temporary files for delta application */
176 59d1e4a0 2021-03-10 stsp
177 59d1e4a0 2021-03-10 stsp basefd = got_opentempfd();
178 59d1e4a0 2021-03-10 stsp if (basefd == -1)
179 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
180 59d1e4a0 2021-03-10 stsp
181 59d1e4a0 2021-03-10 stsp accumfd = got_opentempfd();
182 59d1e4a0 2021-03-10 stsp if (accumfd == -1) {
183 59d1e4a0 2021-03-10 stsp close(basefd);
184 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
185 59d1e4a0 2021-03-10 stsp }
186 59d1e4a0 2021-03-10 stsp
187 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
188 59d1e4a0 2021-03-10 stsp if (outfd_child == -1) {
189 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("dup");
190 59d1e4a0 2021-03-10 stsp close(basefd);
191 59d1e4a0 2021-03-10 stsp close(accumfd);
192 59d1e4a0 2021-03-10 stsp return err;
193 59d1e4a0 2021-03-10 stsp }
194 59d1e4a0 2021-03-10 stsp
195 59d1e4a0 2021-03-10 stsp err = got_privsep_send_packed_raw_obj_req(ibuf, idx, id);
196 59d1e4a0 2021-03-10 stsp if (err) {
197 59d1e4a0 2021-03-10 stsp close(basefd);
198 59d1e4a0 2021-03-10 stsp close(accumfd);
199 59d1e4a0 2021-03-10 stsp close(outfd_child);
200 59d1e4a0 2021-03-10 stsp return err;
201 59d1e4a0 2021-03-10 stsp }
202 59d1e4a0 2021-03-10 stsp
203 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
204 59d1e4a0 2021-03-10 stsp if (err) {
205 59d1e4a0 2021-03-10 stsp close(basefd);
206 59d1e4a0 2021-03-10 stsp close(accumfd);
207 59d1e4a0 2021-03-10 stsp return err;
208 59d1e4a0 2021-03-10 stsp }
209 59d1e4a0 2021-03-10 stsp
210 59d1e4a0 2021-03-10 stsp
211 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
212 59d1e4a0 2021-03-10 stsp basefd);
213 59d1e4a0 2021-03-10 stsp if (err) {
214 59d1e4a0 2021-03-10 stsp close(accumfd);
215 59d1e4a0 2021-03-10 stsp return err;
216 59d1e4a0 2021-03-10 stsp }
217 59d1e4a0 2021-03-10 stsp
218 59d1e4a0 2021-03-10 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
219 59d1e4a0 2021-03-10 stsp accumfd);
220 59d1e4a0 2021-03-10 stsp if (err)
221 59d1e4a0 2021-03-10 stsp return err;
222 59d1e4a0 2021-03-10 stsp
223 59d1e4a0 2021-03-10 stsp err = got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
224 59d1e4a0 2021-03-10 stsp if (err)
225 59d1e4a0 2021-03-10 stsp return err;
226 59d1e4a0 2021-03-10 stsp
227 59d1e4a0 2021-03-10 stsp return NULL;
228 59d1e4a0 2021-03-10 stsp }
229 59d1e4a0 2021-03-10 stsp
230 da506691 2019-05-22 stsp static void
231 b48e2ddb 2019-05-22 stsp set_max_datasize(void)
232 b48e2ddb 2019-05-22 stsp {
233 b48e2ddb 2019-05-22 stsp struct rlimit rl;
234 b48e2ddb 2019-05-22 stsp
235 b48e2ddb 2019-05-22 stsp if (getrlimit(RLIMIT_DATA, &rl) != 0)
236 b48e2ddb 2019-05-22 stsp return;
237 b48e2ddb 2019-05-22 stsp
238 b48e2ddb 2019-05-22 stsp rl.rlim_cur = rl.rlim_max;
239 b48e2ddb 2019-05-22 stsp setrlimit(RLIMIT_DATA, &rl);
240 a158c901 2018-12-23 stsp }
241 a158c901 2018-12-23 stsp
242 a158c901 2018-12-23 stsp static const struct got_error *
243 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
244 a158c901 2018-12-23 stsp {
245 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
246 a158c901 2018-12-23 stsp int imsg_fds[2];
247 a158c901 2018-12-23 stsp pid_t pid;
248 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
249 a158c901 2018-12-23 stsp
250 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
251 a158c901 2018-12-23 stsp if (ibuf == NULL)
252 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
253 a158c901 2018-12-23 stsp
254 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
255 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
256 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
257 a158c901 2018-12-23 stsp free(ibuf);
258 a158c901 2018-12-23 stsp return err;
259 a158c901 2018-12-23 stsp }
260 a158c901 2018-12-23 stsp
261 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
262 638f9024 2019-05-13 stsp err = got_error_from_errno("socketpair");
263 a158c901 2018-12-23 stsp goto done;
264 a158c901 2018-12-23 stsp }
265 a158c901 2018-12-23 stsp
266 a158c901 2018-12-23 stsp pid = fork();
267 a158c901 2018-12-23 stsp if (pid == -1) {
268 638f9024 2019-05-13 stsp err = got_error_from_errno("fork");
269 a158c901 2018-12-23 stsp goto done;
270 a158c901 2018-12-23 stsp } else if (pid == 0) {
271 b48e2ddb 2019-05-22 stsp set_max_datasize();
272 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
273 a158c901 2018-12-23 stsp pack->path_packfile);
274 a158c901 2018-12-23 stsp /* not reached */
275 a158c901 2018-12-23 stsp }
276 a158c901 2018-12-23 stsp
277 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1)
278 638f9024 2019-05-13 stsp return got_error_from_errno("close");
279 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
280 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
281 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
282 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
283 a158c901 2018-12-23 stsp
284 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
285 a158c901 2018-12-23 stsp if (err) {
286 a158c901 2018-12-23 stsp const struct got_error *child_err;
287 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
288 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
289 a158c901 2018-12-23 stsp pack->privsep_child->pid);
290 a158c901 2018-12-23 stsp if (child_err && err == NULL)
291 a158c901 2018-12-23 stsp err = child_err;
292 a158c901 2018-12-23 stsp }
293 a158c901 2018-12-23 stsp done:
294 a158c901 2018-12-23 stsp if (err) {
295 a158c901 2018-12-23 stsp free(ibuf);
296 a158c901 2018-12-23 stsp free(pack->privsep_child);
297 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
298 711fb6e8 2018-12-23 stsp }
299 a158c901 2018-12-23 stsp return err;
300 a158c901 2018-12-23 stsp }
301 a158c901 2018-12-23 stsp
302 711fb6e8 2018-12-23 stsp static const struct got_error *
303 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
304 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
305 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
306 711fb6e8 2018-12-23 stsp {
307 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
308 a158c901 2018-12-23 stsp
309 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
310 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
311 59d1e4a0 2021-03-10 stsp if (err)
312 59d1e4a0 2021-03-10 stsp return err;
313 59d1e4a0 2021-03-10 stsp }
314 711fb6e8 2018-12-23 stsp
315 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
316 711fb6e8 2018-12-23 stsp }
317 711fb6e8 2018-12-23 stsp
318 59d1e4a0 2021-03-10 stsp static const struct got_error *
319 59d1e4a0 2021-03-10 stsp read_packed_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
320 59d1e4a0 2021-03-10 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
321 59d1e4a0 2021-03-10 stsp struct got_object_id *id)
322 59d1e4a0 2021-03-10 stsp {
323 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
324 711fb6e8 2018-12-23 stsp
325 59d1e4a0 2021-03-10 stsp if (pack->privsep_child == NULL) {
326 59d1e4a0 2021-03-10 stsp err = start_pack_privsep_child(pack, packidx);
327 59d1e4a0 2021-03-10 stsp if (err)
328 59d1e4a0 2021-03-10 stsp return err;
329 59d1e4a0 2021-03-10 stsp }
330 59d1e4a0 2021-03-10 stsp
331 59d1e4a0 2021-03-10 stsp return request_packed_object_raw(outbuf, size, hdrlen, outfd, pack,
332 59d1e4a0 2021-03-10 stsp idx, id);
333 59d1e4a0 2021-03-10 stsp }
334 59d1e4a0 2021-03-10 stsp
335 a158c901 2018-12-23 stsp static const struct got_error *
336 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
337 2090a03d 2018-09-09 stsp struct got_repository *repo)
338 2090a03d 2018-09-09 stsp {
339 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
340 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
341 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
342 2090a03d 2018-09-09 stsp int idx;
343 2090a03d 2018-09-09 stsp char *path_packfile;
344 2090a03d 2018-09-09 stsp
345 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
346 2090a03d 2018-09-09 stsp if (err)
347 2090a03d 2018-09-09 stsp return err;
348 2090a03d 2018-09-09 stsp
349 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
350 2090a03d 2018-09-09 stsp if (err)
351 2090a03d 2018-09-09 stsp return err;
352 2090a03d 2018-09-09 stsp
353 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
354 2090a03d 2018-09-09 stsp if (pack == NULL) {
355 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
356 2090a03d 2018-09-09 stsp if (err)
357 2090a03d 2018-09-09 stsp goto done;
358 2090a03d 2018-09-09 stsp }
359 2090a03d 2018-09-09 stsp
360 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
361 2090a03d 2018-09-09 stsp if (err)
362 2090a03d 2018-09-09 stsp goto done;
363 2090a03d 2018-09-09 stsp done:
364 2090a03d 2018-09-09 stsp free(path_packfile);
365 4558fcd4 2018-01-14 stsp return err;
366 9f2369b0 2018-12-24 stsp }
367 9f2369b0 2018-12-24 stsp
368 9f2369b0 2018-12-24 stsp static const struct got_error *
369 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
370 9f2369b0 2018-12-24 stsp {
371 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
372 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
373 9f2369b0 2018-12-24 stsp
374 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
375 9f2369b0 2018-12-24 stsp
376 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
377 9f2369b0 2018-12-24 stsp if (err)
378 9f2369b0 2018-12-24 stsp return err;
379 9f2369b0 2018-12-24 stsp
380 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
381 9f2369b0 2018-12-24 stsp }
382 9f2369b0 2018-12-24 stsp
383 9f2369b0 2018-12-24 stsp static const struct got_error *
384 59d1e4a0 2021-03-10 stsp request_raw_object(uint8_t **outbuf, off_t *size, size_t *hdrlen, int outfd,
385 59d1e4a0 2021-03-10 stsp struct got_repository *repo, int infd)
386 9f2369b0 2018-12-24 stsp {
387 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
388 59d1e4a0 2021-03-10 stsp struct imsgbuf *ibuf;
389 59d1e4a0 2021-03-10 stsp int outfd_child;
390 59d1e4a0 2021-03-10 stsp
391 59d1e4a0 2021-03-10 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
392 59d1e4a0 2021-03-10 stsp
393 59d1e4a0 2021-03-10 stsp outfd_child = dup(outfd);
394 59d1e4a0 2021-03-10 stsp if (outfd_child == -1)
395 59d1e4a0 2021-03-10 stsp return got_error_from_errno("dup");
396 59d1e4a0 2021-03-10 stsp
397 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_req(ibuf, infd);
398 59d1e4a0 2021-03-10 stsp if (err)
399 59d1e4a0 2021-03-10 stsp return err;
400 59d1e4a0 2021-03-10 stsp
401 59d1e4a0 2021-03-10 stsp err = got_privsep_send_raw_obj_outfd(ibuf, outfd_child);
402 59d1e4a0 2021-03-10 stsp if (err)
403 59d1e4a0 2021-03-10 stsp return err;
404 59d1e4a0 2021-03-10 stsp
405 59d1e4a0 2021-03-10 stsp return got_privsep_recv_raw_obj(outbuf, size, hdrlen, ibuf);
406 59d1e4a0 2021-03-10 stsp }
407 59d1e4a0 2021-03-10 stsp
408 59d1e4a0 2021-03-10 stsp static const struct got_error *
409 59d1e4a0 2021-03-10 stsp start_read_object_child(struct got_repository *repo)
410 59d1e4a0 2021-03-10 stsp {
411 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
412 9f2369b0 2018-12-24 stsp int imsg_fds[2];
413 9f2369b0 2018-12-24 stsp pid_t pid;
414 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
415 9f2369b0 2018-12-24 stsp
416 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
417 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
418 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
419 9f2369b0 2018-12-24 stsp
420 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
421 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
422 ddc7b220 2019-09-08 stsp free(ibuf);
423 ddc7b220 2019-09-08 stsp return err;
424 ddc7b220 2019-09-08 stsp }
425 9f2369b0 2018-12-24 stsp
426 9f2369b0 2018-12-24 stsp pid = fork();
427 ddc7b220 2019-09-08 stsp if (pid == -1) {
428 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
429 ddc7b220 2019-09-08 stsp free(ibuf);
430 ddc7b220 2019-09-08 stsp return err;
431 ddc7b220 2019-09-08 stsp }
432 9f2369b0 2018-12-24 stsp else if (pid == 0) {
433 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
434 9f2369b0 2018-12-24 stsp repo->path);
435 9f2369b0 2018-12-24 stsp /* not reached */
436 9f2369b0 2018-12-24 stsp }
437 9f2369b0 2018-12-24 stsp
438 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
439 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
440 ddc7b220 2019-09-08 stsp free(ibuf);
441 ddc7b220 2019-09-08 stsp return err;
442 ddc7b220 2019-09-08 stsp }
443 59d1e4a0 2021-03-10 stsp
444 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
445 9f2369b0 2018-12-24 stsp imsg_fds[0];
446 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
447 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
448 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
449 9f2369b0 2018-12-24 stsp
450 59d1e4a0 2021-03-10 stsp return NULL;
451 59d1e4a0 2021-03-10 stsp }
452 59d1e4a0 2021-03-10 stsp
453 59d1e4a0 2021-03-10 stsp static const struct got_error *
454 59d1e4a0 2021-03-10 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
455 59d1e4a0 2021-03-10 stsp int obj_fd)
456 59d1e4a0 2021-03-10 stsp {
457 59d1e4a0 2021-03-10 stsp const struct got_error *err;
458 59d1e4a0 2021-03-10 stsp
459 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
460 59d1e4a0 2021-03-10 stsp return request_object(obj, repo, obj_fd);
461 59d1e4a0 2021-03-10 stsp
462 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
463 7495ec13 2021-04-04 stsp if (err) {
464 7495ec13 2021-04-04 stsp close(obj_fd);
465 59d1e4a0 2021-03-10 stsp return err;
466 7495ec13 2021-04-04 stsp }
467 59d1e4a0 2021-03-10 stsp
468 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
469 4558fcd4 2018-01-14 stsp }
470 a1fd68d8 2018-01-12 stsp
471 59d1e4a0 2021-03-10 stsp static const struct got_error *
472 59d1e4a0 2021-03-10 stsp read_object_raw_privsep(uint8_t **outbuf, off_t *size, size_t *hdrlen,
473 59d1e4a0 2021-03-10 stsp int outfd, struct got_repository *repo, int obj_fd)
474 59d1e4a0 2021-03-10 stsp {
475 59d1e4a0 2021-03-10 stsp const struct got_error *err;
476 59d1e4a0 2021-03-10 stsp
477 59d1e4a0 2021-03-10 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
478 59d1e4a0 2021-03-10 stsp return request_raw_object(outbuf, size, hdrlen, outfd, repo,
479 59d1e4a0 2021-03-10 stsp obj_fd);
480 59d1e4a0 2021-03-10 stsp
481 59d1e4a0 2021-03-10 stsp err = start_read_object_child(repo);
482 59d1e4a0 2021-03-10 stsp if (err)
483 59d1e4a0 2021-03-10 stsp return err;
484 59d1e4a0 2021-03-10 stsp
485 59d1e4a0 2021-03-10 stsp return request_raw_object(outbuf, size, hdrlen, outfd, repo, obj_fd);
486 59d1e4a0 2021-03-10 stsp }
487 9f2369b0 2018-12-24 stsp
488 4558fcd4 2018-01-14 stsp const struct got_error *
489 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
490 4558fcd4 2018-01-14 stsp struct got_object_id *id)
491 4558fcd4 2018-01-14 stsp {
492 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
493 2178c42e 2018-04-22 stsp int fd;
494 7bb0daa1 2018-06-21 stsp
495 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
496 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
497 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
498 7bb0daa1 2018-06-21 stsp return NULL;
499 7bb0daa1 2018-06-21 stsp }
500 4558fcd4 2018-01-14 stsp
501 e82b1d81 2019-07-27 stsp err = open_packed_object(obj, id, repo);
502 e82b1d81 2019-07-27 stsp if (err && err->code != GOT_ERR_NO_OBJ)
503 e82b1d81 2019-07-27 stsp return err;
504 e82b1d81 2019-07-27 stsp if (*obj) {
505 e82b1d81 2019-07-27 stsp (*obj)->refcnt++;
506 e82b1d81 2019-07-27 stsp return got_repo_cache_object(repo, id, *obj);
507 e82b1d81 2019-07-27 stsp }
508 e82b1d81 2019-07-27 stsp
509 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
510 762d73f4 2021-04-10 stsp if (err) {
511 762d73f4 2021-04-10 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
512 762d73f4 2021-04-10 stsp err = got_error_no_obj(id);
513 762d73f4 2021-04-10 stsp return err;
514 762d73f4 2021-04-10 stsp }
515 762d73f4 2021-04-10 stsp
516 762d73f4 2021-04-10 stsp err = read_object_header_privsep(obj, repo, fd);
517 4558fcd4 2018-01-14 stsp if (err)
518 4558fcd4 2018-01-14 stsp return err;
519 4558fcd4 2018-01-14 stsp
520 762d73f4 2021-04-10 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
521 7bb0daa1 2018-06-21 stsp
522 59790a32 2018-09-15 stsp (*obj)->refcnt++;
523 762d73f4 2021-04-10 stsp return got_repo_cache_object(repo, id, *obj);
524 59d1e4a0 2021-03-10 stsp }
525 6c00b545 2018-01-17 stsp
526 59d1e4a0 2021-03-10 stsp const struct got_error *
527 59d1e4a0 2021-03-10 stsp got_object_raw_open(struct got_raw_object **obj, struct got_repository *repo,
528 59d1e4a0 2021-03-10 stsp struct got_object_id *id, size_t blocksize)
529 59d1e4a0 2021-03-10 stsp {
530 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
531 59d1e4a0 2021-03-10 stsp struct got_packidx *packidx = NULL;
532 59d1e4a0 2021-03-10 stsp int idx;
533 59d1e4a0 2021-03-10 stsp uint8_t *outbuf = NULL;
534 59d1e4a0 2021-03-10 stsp int outfd = -1;
535 59d1e4a0 2021-03-10 stsp off_t size = 0;
536 59d1e4a0 2021-03-10 stsp size_t hdrlen = 0;
537 59d1e4a0 2021-03-10 stsp char *path_packfile = NULL;
538 59d1e4a0 2021-03-10 stsp
539 59d1e4a0 2021-03-10 stsp *obj = NULL;
540 59d1e4a0 2021-03-10 stsp
541 59d1e4a0 2021-03-10 stsp outfd = got_opentempfd();
542 59d1e4a0 2021-03-10 stsp if (outfd == -1)
543 59d1e4a0 2021-03-10 stsp return got_error_from_errno("got_opentempfd");
544 59d1e4a0 2021-03-10 stsp
545 59d1e4a0 2021-03-10 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
546 59d1e4a0 2021-03-10 stsp if (err == NULL) {
547 59d1e4a0 2021-03-10 stsp struct got_pack *pack = NULL;
548 59d1e4a0 2021-03-10 stsp
549 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
550 59d1e4a0 2021-03-10 stsp if (err)
551 59d1e4a0 2021-03-10 stsp goto done;
552 59d1e4a0 2021-03-10 stsp
553 59d1e4a0 2021-03-10 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
554 59d1e4a0 2021-03-10 stsp if (pack == NULL) {
555 59d1e4a0 2021-03-10 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
556 59d1e4a0 2021-03-10 stsp packidx);
557 59d1e4a0 2021-03-10 stsp if (err)
558 59d1e4a0 2021-03-10 stsp goto done;
559 59d1e4a0 2021-03-10 stsp }
560 59d1e4a0 2021-03-10 stsp err = read_packed_object_raw_privsep(&outbuf, &size, &hdrlen,
561 59d1e4a0 2021-03-10 stsp outfd, pack, packidx, idx, id);
562 59d1e4a0 2021-03-10 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
563 59d1e4a0 2021-03-10 stsp int fd;
564 59d1e4a0 2021-03-10 stsp
565 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
566 59d1e4a0 2021-03-10 stsp if (err)
567 59d1e4a0 2021-03-10 stsp goto done;
568 59d1e4a0 2021-03-10 stsp err = read_object_raw_privsep(&outbuf, &size, &hdrlen, outfd,
569 59d1e4a0 2021-03-10 stsp repo, fd);
570 59d1e4a0 2021-03-10 stsp }
571 59d1e4a0 2021-03-10 stsp
572 59d1e4a0 2021-03-10 stsp if (hdrlen > size) {
573 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
574 59d1e4a0 2021-03-10 stsp goto done;
575 59d1e4a0 2021-03-10 stsp }
576 59d1e4a0 2021-03-10 stsp
577 59d1e4a0 2021-03-10 stsp *obj = calloc(1, sizeof(**obj));
578 59d1e4a0 2021-03-10 stsp if (*obj == NULL) {
579 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("calloc");
580 59d1e4a0 2021-03-10 stsp goto done;
581 59d1e4a0 2021-03-10 stsp }
582 59d1e4a0 2021-03-10 stsp
583 59d1e4a0 2021-03-10 stsp (*obj)->read_buf = malloc(blocksize);
584 59d1e4a0 2021-03-10 stsp if ((*obj)->read_buf == NULL) {
585 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("malloc");
586 59d1e4a0 2021-03-10 stsp goto done;
587 59d1e4a0 2021-03-10 stsp }
588 59d1e4a0 2021-03-10 stsp
589 59d1e4a0 2021-03-10 stsp if (outbuf) {
590 59d1e4a0 2021-03-10 stsp if (close(outfd) == -1) {
591 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("close");
592 59d1e4a0 2021-03-10 stsp goto done;
593 59d1e4a0 2021-03-10 stsp }
594 59d1e4a0 2021-03-10 stsp outfd = -1;
595 59d1e4a0 2021-03-10 stsp (*obj)->f = fmemopen(outbuf, hdrlen + size, "r");
596 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
597 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
598 59d1e4a0 2021-03-10 stsp goto done;
599 59d1e4a0 2021-03-10 stsp }
600 59d1e4a0 2021-03-10 stsp (*obj)->data = outbuf;
601 59d1e4a0 2021-03-10 stsp } else {
602 59d1e4a0 2021-03-10 stsp struct stat sb;
603 59d1e4a0 2021-03-10 stsp if (fstat(outfd, &sb) == -1) {
604 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fstat");
605 59d1e4a0 2021-03-10 stsp goto done;
606 59d1e4a0 2021-03-10 stsp }
607 59d1e4a0 2021-03-10 stsp
608 59d1e4a0 2021-03-10 stsp if (sb.st_size != size) {
609 59d1e4a0 2021-03-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
610 59d1e4a0 2021-03-10 stsp goto done;
611 59d1e4a0 2021-03-10 stsp }
612 59d1e4a0 2021-03-10 stsp
613 59d1e4a0 2021-03-10 stsp (*obj)->f = fdopen(outfd, "r");
614 59d1e4a0 2021-03-10 stsp if ((*obj)->f == NULL) {
615 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fdopen");
616 59d1e4a0 2021-03-10 stsp goto done;
617 59d1e4a0 2021-03-10 stsp }
618 59d1e4a0 2021-03-10 stsp outfd = -1;
619 59d1e4a0 2021-03-10 stsp (*obj)->data = NULL;
620 59d1e4a0 2021-03-10 stsp }
621 59d1e4a0 2021-03-10 stsp (*obj)->hdrlen = hdrlen;
622 59d1e4a0 2021-03-10 stsp (*obj)->size = size;
623 59d1e4a0 2021-03-10 stsp (*obj)->blocksize = blocksize;
624 59d1e4a0 2021-03-10 stsp done:
625 59d1e4a0 2021-03-10 stsp free(path_packfile);
626 59d1e4a0 2021-03-10 stsp if (err) {
627 59d1e4a0 2021-03-10 stsp if (*obj) {
628 59d1e4a0 2021-03-10 stsp got_object_raw_close(*obj);
629 59d1e4a0 2021-03-10 stsp *obj = NULL;
630 59d1e4a0 2021-03-10 stsp }
631 59d1e4a0 2021-03-10 stsp if (outfd != -1)
632 59d1e4a0 2021-03-10 stsp close(outfd);
633 59d1e4a0 2021-03-10 stsp free(outbuf);
634 59d1e4a0 2021-03-10 stsp }
635 59d1e4a0 2021-03-10 stsp return err;
636 ab9a70b2 2017-11-06 stsp }
637 ab9a70b2 2017-11-06 stsp
638 59d1e4a0 2021-03-10 stsp void
639 59d1e4a0 2021-03-10 stsp got_object_raw_rewind(struct got_raw_object *obj)
640 59d1e4a0 2021-03-10 stsp {
641 59d1e4a0 2021-03-10 stsp if (obj->f)
642 59d1e4a0 2021-03-10 stsp rewind(obj->f);
643 59d1e4a0 2021-03-10 stsp }
644 59d1e4a0 2021-03-10 stsp
645 59d1e4a0 2021-03-10 stsp size_t
646 59d1e4a0 2021-03-10 stsp got_object_raw_get_hdrlen(struct got_raw_object *obj)
647 59d1e4a0 2021-03-10 stsp {
648 59d1e4a0 2021-03-10 stsp return obj->hdrlen;
649 59d1e4a0 2021-03-10 stsp }
650 59d1e4a0 2021-03-10 stsp
651 59d1e4a0 2021-03-10 stsp const uint8_t *
652 59d1e4a0 2021-03-10 stsp got_object_raw_get_read_buf(struct got_raw_object *obj)
653 59d1e4a0 2021-03-10 stsp {
654 59d1e4a0 2021-03-10 stsp return obj->read_buf;
655 59d1e4a0 2021-03-10 stsp }
656 59d1e4a0 2021-03-10 stsp
657 59d1e4a0 2021-03-10 stsp const struct got_error *
658 59d1e4a0 2021-03-10 stsp got_object_raw_read_block(size_t *outlenp, struct got_raw_object *obj)
659 59d1e4a0 2021-03-10 stsp {
660 59d1e4a0 2021-03-10 stsp size_t n;
661 59d1e4a0 2021-03-10 stsp
662 59d1e4a0 2021-03-10 stsp n = fread(obj->read_buf, 1, obj->blocksize, obj->f);
663 59d1e4a0 2021-03-10 stsp if (n == 0 && ferror(obj->f))
664 59d1e4a0 2021-03-10 stsp return got_ferror(obj->f, GOT_ERR_IO);
665 59d1e4a0 2021-03-10 stsp *outlenp = n;
666 59d1e4a0 2021-03-10 stsp return NULL;
667 59d1e4a0 2021-03-10 stsp }
668 59d1e4a0 2021-03-10 stsp
669 59d1e4a0 2021-03-10 stsp const struct got_error *
670 59d1e4a0 2021-03-10 stsp got_object_raw_close(struct got_raw_object *obj)
671 59d1e4a0 2021-03-10 stsp {
672 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
673 59d1e4a0 2021-03-10 stsp
674 59d1e4a0 2021-03-10 stsp free(obj->read_buf);
675 59d1e4a0 2021-03-10 stsp if (obj->f != NULL && fclose(obj->f) == EOF && err == NULL)
676 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
677 59d1e4a0 2021-03-10 stsp free(obj->data);
678 59d1e4a0 2021-03-10 stsp free(obj);
679 59d1e4a0 2021-03-10 stsp return err;
680 59d1e4a0 2021-03-10 stsp }
681 59d1e4a0 2021-03-10 stsp
682 6dfa2fd3 2018-02-12 stsp const struct got_error *
683 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
684 6dfa2fd3 2018-02-12 stsp const char *id_str)
685 6dfa2fd3 2018-02-12 stsp {
686 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
687 6dfa2fd3 2018-02-12 stsp
688 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
689 6dd1ece6 2019-11-10 stsp return got_error_path(id_str, GOT_ERR_BAD_OBJ_ID_STR);
690 6dfa2fd3 2018-02-12 stsp
691 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
692 15a94983 2018-12-23 stsp }
693 15a94983 2018-12-23 stsp
694 15a94983 2018-12-23 stsp const struct got_error *
695 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
696 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
697 15a94983 2018-12-23 stsp {
698 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
699 15a94983 2018-12-23 stsp struct got_object *obj;
700 15a94983 2018-12-23 stsp
701 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
702 15a94983 2018-12-23 stsp if (err)
703 15a94983 2018-12-23 stsp return err;
704 15a94983 2018-12-23 stsp
705 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
706 15a94983 2018-12-23 stsp got_object_close(obj);
707 15a94983 2018-12-23 stsp if (*id == NULL)
708 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
709 15a94983 2018-12-23 stsp
710 15a94983 2018-12-23 stsp return NULL;
711 434025f3 2018-09-16 stsp }
712 434025f3 2018-09-16 stsp
713 434025f3 2018-09-16 stsp static const struct got_error *
714 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
715 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
716 a158c901 2018-12-23 stsp {
717 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
718 a158c901 2018-12-23 stsp
719 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
720 a158c901 2018-12-23 stsp pack_idx);
721 a158c901 2018-12-23 stsp if (err)
722 a158c901 2018-12-23 stsp return err;
723 a158c901 2018-12-23 stsp
724 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
725 ca6e02ac 2020-01-07 stsp if (err)
726 ca6e02ac 2020-01-07 stsp return err;
727 ca6e02ac 2020-01-07 stsp
728 ca6e02ac 2020-01-07 stsp (*commit)->flags |= GOT_COMMIT_FLAG_PACKED;
729 ca6e02ac 2020-01-07 stsp return NULL;
730 a158c901 2018-12-23 stsp }
731 a158c901 2018-12-23 stsp
732 a158c901 2018-12-23 stsp static const struct got_error *
733 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
734 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
735 a158c901 2018-12-23 stsp struct got_object_id *id)
736 a158c901 2018-12-23 stsp {
737 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
738 a158c901 2018-12-23 stsp
739 a158c901 2018-12-23 stsp if (pack->privsep_child)
740 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
741 a158c901 2018-12-23 stsp
742 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
743 711fb6e8 2018-12-23 stsp if (err)
744 a158c901 2018-12-23 stsp return err;
745 a158c901 2018-12-23 stsp
746 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
747 9f2369b0 2018-12-24 stsp }
748 9f2369b0 2018-12-24 stsp
749 9f2369b0 2018-12-24 stsp static const struct got_error *
750 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
751 9f2369b0 2018-12-24 stsp int fd)
752 9f2369b0 2018-12-24 stsp {
753 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
754 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
755 9f2369b0 2018-12-24 stsp
756 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
757 9f2369b0 2018-12-24 stsp
758 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
759 9f2369b0 2018-12-24 stsp if (err)
760 9f2369b0 2018-12-24 stsp return err;
761 9f2369b0 2018-12-24 stsp
762 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
763 9f2369b0 2018-12-24 stsp }
764 9f2369b0 2018-12-24 stsp
765 9f2369b0 2018-12-24 stsp static const struct got_error *
766 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
767 9f2369b0 2018-12-24 stsp struct got_repository *repo)
768 9f2369b0 2018-12-24 stsp {
769 ddc7b220 2019-09-08 stsp const struct got_error *err;
770 9f2369b0 2018-12-24 stsp int imsg_fds[2];
771 9f2369b0 2018-12-24 stsp pid_t pid;
772 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
773 9f2369b0 2018-12-24 stsp
774 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
775 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
776 9f2369b0 2018-12-24 stsp
777 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
778 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
779 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
780 9f2369b0 2018-12-24 stsp
781 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
782 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
783 ddc7b220 2019-09-08 stsp free(ibuf);
784 ddc7b220 2019-09-08 stsp return err;
785 ddc7b220 2019-09-08 stsp }
786 9f2369b0 2018-12-24 stsp
787 9f2369b0 2018-12-24 stsp pid = fork();
788 ddc7b220 2019-09-08 stsp if (pid == -1) {
789 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
790 ddc7b220 2019-09-08 stsp free(ibuf);
791 ddc7b220 2019-09-08 stsp return err;
792 ddc7b220 2019-09-08 stsp }
793 9f2369b0 2018-12-24 stsp else if (pid == 0) {
794 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
795 9f2369b0 2018-12-24 stsp repo->path);
796 9f2369b0 2018-12-24 stsp /* not reached */
797 9f2369b0 2018-12-24 stsp }
798 9f2369b0 2018-12-24 stsp
799 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
800 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
801 ddc7b220 2019-09-08 stsp free(ibuf);
802 ddc7b220 2019-09-08 stsp return err;
803 ddc7b220 2019-09-08 stsp }
804 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
805 9f2369b0 2018-12-24 stsp imsg_fds[0];
806 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
807 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
808 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
809 9f2369b0 2018-12-24 stsp
810 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
811 a158c901 2018-12-23 stsp }
812 a158c901 2018-12-23 stsp
813 9f2369b0 2018-12-24 stsp
814 a158c901 2018-12-23 stsp static const struct got_error *
815 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
816 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
817 434025f3 2018-09-16 stsp {
818 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
819 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
820 e82b1d81 2019-07-27 stsp int idx;
821 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
822 434025f3 2018-09-16 stsp
823 434025f3 2018-09-16 stsp if (check_cache) {
824 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
825 434025f3 2018-09-16 stsp if (*commit != NULL) {
826 434025f3 2018-09-16 stsp (*commit)->refcnt++;
827 434025f3 2018-09-16 stsp return NULL;
828 434025f3 2018-09-16 stsp }
829 434025f3 2018-09-16 stsp } else
830 434025f3 2018-09-16 stsp *commit = NULL;
831 434025f3 2018-09-16 stsp
832 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
833 e82b1d81 2019-07-27 stsp if (err == NULL) {
834 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
835 34f480ff 2019-07-27 stsp
836 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
837 1785f84a 2018-12-23 stsp if (err)
838 1785f84a 2018-12-23 stsp return err;
839 1785f84a 2018-12-23 stsp
840 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
841 434025f3 2018-09-16 stsp if (pack == NULL) {
842 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
843 1785f84a 2018-12-23 stsp packidx);
844 434025f3 2018-09-16 stsp if (err)
845 8d2c5ea3 2019-08-13 stsp goto done;
846 434025f3 2018-09-16 stsp }
847 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
848 1785f84a 2018-12-23 stsp packidx, idx, id);
849 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
850 e82b1d81 2019-07-27 stsp int fd;
851 e82b1d81 2019-07-27 stsp
852 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
853 e82b1d81 2019-07-27 stsp if (err)
854 e82b1d81 2019-07-27 stsp return err;
855 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
856 e82b1d81 2019-07-27 stsp }
857 434025f3 2018-09-16 stsp
858 434025f3 2018-09-16 stsp if (err == NULL) {
859 434025f3 2018-09-16 stsp (*commit)->refcnt++;
860 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
861 e32baab7 2018-11-05 stsp }
862 8d2c5ea3 2019-08-13 stsp done:
863 8d2c5ea3 2019-08-13 stsp free(path_packfile);
864 e32baab7 2018-11-05 stsp return err;
865 e32baab7 2018-11-05 stsp }
866 e32baab7 2018-11-05 stsp
867 e32baab7 2018-11-05 stsp const struct got_error *
868 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
869 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
870 e32baab7 2018-11-05 stsp {
871 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
872 e32baab7 2018-11-05 stsp if (*commit != NULL) {
873 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
874 e32baab7 2018-11-05 stsp return NULL;
875 e32baab7 2018-11-05 stsp }
876 e32baab7 2018-11-05 stsp
877 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
878 7762fe12 2018-11-05 stsp }
879 7762fe12 2018-11-05 stsp
880 e32baab7 2018-11-05 stsp const struct got_error *
881 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
882 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
883 e32baab7 2018-11-05 stsp {
884 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
885 434025f3 2018-09-16 stsp }
886 434025f3 2018-09-16 stsp
887 434025f3 2018-09-16 stsp const struct got_error *
888 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
889 dbc6a6b6 2018-07-12 stsp {
890 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
891 dbc6a6b6 2018-07-12 stsp
892 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
893 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
894 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
895 dbc6a6b6 2018-07-12 stsp
896 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
897 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
898 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
899 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
900 dbc6a6b6 2018-07-12 stsp *qid = NULL;
901 dbc6a6b6 2018-07-12 stsp return err;
902 dbc6a6b6 2018-07-12 stsp }
903 dbc6a6b6 2018-07-12 stsp
904 dbc6a6b6 2018-07-12 stsp return NULL;
905 a158c901 2018-12-23 stsp }
906 a158c901 2018-12-23 stsp
907 a158c901 2018-12-23 stsp static const struct got_error *
908 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
909 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
910 a158c901 2018-12-23 stsp {
911 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
912 a158c901 2018-12-23 stsp
913 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
914 13c729f7 2018-12-24 stsp pack_idx);
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_tree(tree, pack->privsep_child->ibuf);
919 7e212e3d 2018-09-09 stsp }
920 7e212e3d 2018-09-09 stsp
921 71eb0e7f 2018-09-16 stsp static const struct got_error *
922 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
923 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
924 13c729f7 2018-12-24 stsp struct got_object_id *id)
925 13c729f7 2018-12-24 stsp {
926 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
927 13c729f7 2018-12-24 stsp
928 13c729f7 2018-12-24 stsp if (pack->privsep_child)
929 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
930 13c729f7 2018-12-24 stsp
931 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
932 13c729f7 2018-12-24 stsp if (err)
933 13c729f7 2018-12-24 stsp return err;
934 13c729f7 2018-12-24 stsp
935 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
936 13c729f7 2018-12-24 stsp }
937 13c729f7 2018-12-24 stsp
938 13c729f7 2018-12-24 stsp static const struct got_error *
939 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
940 9f2369b0 2018-12-24 stsp int fd)
941 9f2369b0 2018-12-24 stsp {
942 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
943 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
944 9f2369b0 2018-12-24 stsp
945 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
946 9f2369b0 2018-12-24 stsp
947 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
948 9f2369b0 2018-12-24 stsp if (err)
949 9f2369b0 2018-12-24 stsp return err;
950 9f2369b0 2018-12-24 stsp
951 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
952 9f2369b0 2018-12-24 stsp }
953 9f2369b0 2018-12-24 stsp
954 9f2369b0 2018-12-24 stsp const struct got_error *
955 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
956 9f2369b0 2018-12-24 stsp struct got_repository *repo)
957 9f2369b0 2018-12-24 stsp {
958 ddc7b220 2019-09-08 stsp const struct got_error *err;
959 9f2369b0 2018-12-24 stsp int imsg_fds[2];
960 9f2369b0 2018-12-24 stsp pid_t pid;
961 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
962 9f2369b0 2018-12-24 stsp
963 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
964 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
965 9f2369b0 2018-12-24 stsp
966 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
967 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
968 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
969 9f2369b0 2018-12-24 stsp
970 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
971 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
972 ddc7b220 2019-09-08 stsp free(ibuf);
973 ddc7b220 2019-09-08 stsp return err;
974 ddc7b220 2019-09-08 stsp }
975 9f2369b0 2018-12-24 stsp
976 9f2369b0 2018-12-24 stsp pid = fork();
977 ddc7b220 2019-09-08 stsp if (pid == -1) {
978 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
979 ddc7b220 2019-09-08 stsp free(ibuf);
980 ddc7b220 2019-09-08 stsp return err;
981 ddc7b220 2019-09-08 stsp }
982 9f2369b0 2018-12-24 stsp else if (pid == 0) {
983 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
984 9f2369b0 2018-12-24 stsp repo->path);
985 9f2369b0 2018-12-24 stsp /* not reached */
986 9f2369b0 2018-12-24 stsp }
987 9f2369b0 2018-12-24 stsp
988 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
989 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
990 ddc7b220 2019-09-08 stsp free(ibuf);
991 ddc7b220 2019-09-08 stsp return err;
992 ddc7b220 2019-09-08 stsp }
993 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
994 9f2369b0 2018-12-24 stsp imsg_fds[0];
995 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
996 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
997 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
998 9f2369b0 2018-12-24 stsp
999 9f2369b0 2018-12-24 stsp
1000 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
1001 9f2369b0 2018-12-24 stsp }
1002 9f2369b0 2018-12-24 stsp
1003 9f2369b0 2018-12-24 stsp static const struct got_error *
1004 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
1005 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
1006 0ffeb3c2 2017-11-26 stsp {
1007 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
1008 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
1009 e82b1d81 2019-07-27 stsp int idx;
1010 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1011 f6be5c30 2018-06-22 stsp
1012 71eb0e7f 2018-09-16 stsp if (check_cache) {
1013 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
1014 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
1015 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
1016 71eb0e7f 2018-09-16 stsp return NULL;
1017 71eb0e7f 2018-09-16 stsp }
1018 71eb0e7f 2018-09-16 stsp } else
1019 71eb0e7f 2018-09-16 stsp *tree = NULL;
1020 0ffeb3c2 2017-11-26 stsp
1021 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1022 e82b1d81 2019-07-27 stsp if (err == NULL) {
1023 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
1024 0ffeb3c2 2017-11-26 stsp
1025 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1026 13c729f7 2018-12-24 stsp if (err)
1027 13c729f7 2018-12-24 stsp return err;
1028 13c729f7 2018-12-24 stsp
1029 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1030 e7885405 2018-09-10 stsp if (pack == NULL) {
1031 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1032 e82b1d81 2019-07-27 stsp packidx);
1033 e7885405 2018-09-10 stsp if (err)
1034 8d2c5ea3 2019-08-13 stsp goto done;
1035 e7885405 2018-09-10 stsp }
1036 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
1037 13c729f7 2018-12-24 stsp packidx, idx, id);
1038 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1039 e82b1d81 2019-07-27 stsp int fd;
1040 e82b1d81 2019-07-27 stsp
1041 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1042 e82b1d81 2019-07-27 stsp if (err)
1043 e82b1d81 2019-07-27 stsp return err;
1044 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
1045 e82b1d81 2019-07-27 stsp }
1046 f6be5c30 2018-06-22 stsp
1047 f6be5c30 2018-06-22 stsp if (err == NULL) {
1048 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
1049 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
1050 f6be5c30 2018-06-22 stsp }
1051 8d2c5ea3 2019-08-13 stsp done:
1052 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1053 776d4d29 2018-06-17 stsp return err;
1054 776d4d29 2018-06-17 stsp }
1055 776d4d29 2018-06-17 stsp
1056 776d4d29 2018-06-17 stsp const struct got_error *
1057 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
1058 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
1059 776d4d29 2018-06-17 stsp {
1060 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
1061 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
1062 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
1063 e8eb494a 2018-09-16 stsp return NULL;
1064 e0ab43e7 2018-03-16 stsp }
1065 776d4d29 2018-06-17 stsp
1066 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
1067 57efb1af 2018-04-24 stsp }
1068 ff6b18f8 2018-04-24 stsp
1069 71eb0e7f 2018-09-16 stsp const struct got_error *
1070 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
1071 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
1072 71eb0e7f 2018-09-16 stsp {
1073 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
1074 71eb0e7f 2018-09-16 stsp }
1075 71eb0e7f 2018-09-16 stsp
1076 56e0773d 2019-11-28 stsp int
1077 56e0773d 2019-11-28 stsp got_object_tree_get_nentries(struct got_tree_object *tree)
1078 883f0469 2018-06-23 stsp {
1079 56e0773d 2019-11-28 stsp return tree->nentries;
1080 56e0773d 2019-11-28 stsp }
1081 56e0773d 2019-11-28 stsp
1082 56e0773d 2019-11-28 stsp struct got_tree_entry *
1083 56e0773d 2019-11-28 stsp got_object_tree_get_first_entry(struct got_tree_object *tree)
1084 56e0773d 2019-11-28 stsp {
1085 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, 0);
1086 56e0773d 2019-11-28 stsp }
1087 56e0773d 2019-11-28 stsp
1088 56e0773d 2019-11-28 stsp struct got_tree_entry *
1089 56e0773d 2019-11-28 stsp got_object_tree_get_last_entry(struct got_tree_object *tree)
1090 56e0773d 2019-11-28 stsp {
1091 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, tree->nentries - 1);
1092 56e0773d 2019-11-28 stsp }
1093 56e0773d 2019-11-28 stsp
1094 56e0773d 2019-11-28 stsp struct got_tree_entry *
1095 56e0773d 2019-11-28 stsp got_object_tree_get_entry(struct got_tree_object *tree, int i)
1096 56e0773d 2019-11-28 stsp {
1097 56e0773d 2019-11-28 stsp if (i < 0 || i >= tree->nentries)
1098 56e0773d 2019-11-28 stsp return NULL;
1099 56e0773d 2019-11-28 stsp return &tree->entries[i];
1100 883f0469 2018-06-23 stsp }
1101 3840f4c9 2018-09-12 stsp
1102 56e0773d 2019-11-28 stsp mode_t
1103 56e0773d 2019-11-28 stsp got_tree_entry_get_mode(struct got_tree_entry *te)
1104 56e0773d 2019-11-28 stsp {
1105 56e0773d 2019-11-28 stsp return te->mode;
1106 56e0773d 2019-11-28 stsp }
1107 56e0773d 2019-11-28 stsp
1108 56e0773d 2019-11-28 stsp const char *
1109 56e0773d 2019-11-28 stsp got_tree_entry_get_name(struct got_tree_entry *te)
1110 56e0773d 2019-11-28 stsp {
1111 56e0773d 2019-11-28 stsp return &te->name[0];
1112 56e0773d 2019-11-28 stsp }
1113 56e0773d 2019-11-28 stsp
1114 56e0773d 2019-11-28 stsp struct got_object_id *
1115 56e0773d 2019-11-28 stsp got_tree_entry_get_id(struct got_tree_entry *te)
1116 56e0773d 2019-11-28 stsp {
1117 56e0773d 2019-11-28 stsp return &te->id;
1118 0d6c6ee3 2020-05-20 stsp }
1119 0d6c6ee3 2020-05-20 stsp
1120 0d6c6ee3 2020-05-20 stsp const struct got_error *
1121 af57b12a 2020-07-23 stsp got_object_blob_read_to_str(char **s, struct got_blob_object *blob)
1122 0d6c6ee3 2020-05-20 stsp {
1123 0d6c6ee3 2020-05-20 stsp const struct got_error *err = NULL;
1124 32596e16 2020-07-23 stsp size_t len, totlen, hdrlen, offset;
1125 aa092692 2020-07-23 stsp
1126 aa092692 2020-07-23 stsp *s = NULL;
1127 0d6c6ee3 2020-05-20 stsp
1128 659dc16e 2020-07-23 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1129 659dc16e 2020-07-23 stsp totlen = 0;
1130 32596e16 2020-07-23 stsp offset = 0;
1131 659dc16e 2020-07-23 stsp do {
1132 659dc16e 2020-07-23 stsp char *p;
1133 0d6c6ee3 2020-05-20 stsp
1134 659dc16e 2020-07-23 stsp err = got_object_blob_read_block(&len, blob);
1135 659dc16e 2020-07-23 stsp if (err)
1136 af57b12a 2020-07-23 stsp return err;
1137 659dc16e 2020-07-23 stsp
1138 659dc16e 2020-07-23 stsp if (len == 0)
1139 659dc16e 2020-07-23 stsp break;
1140 659dc16e 2020-07-23 stsp
1141 659dc16e 2020-07-23 stsp totlen += len - hdrlen;
1142 af57b12a 2020-07-23 stsp p = realloc(*s, totlen + 1);
1143 659dc16e 2020-07-23 stsp if (p == NULL) {
1144 659dc16e 2020-07-23 stsp err = got_error_from_errno("realloc");
1145 af57b12a 2020-07-23 stsp free(*s);
1146 af57b12a 2020-07-23 stsp *s = NULL;
1147 af57b12a 2020-07-23 stsp return err;
1148 659dc16e 2020-07-23 stsp }
1149 af57b12a 2020-07-23 stsp *s = p;
1150 659dc16e 2020-07-23 stsp /* Skip blob object header first time around. */
1151 af57b12a 2020-07-23 stsp memcpy(*s + offset,
1152 f8f7c882 2020-07-23 stsp got_object_blob_get_read_buf(blob) + hdrlen, len - hdrlen);
1153 659dc16e 2020-07-23 stsp hdrlen = 0;
1154 32596e16 2020-07-23 stsp offset = totlen;
1155 659dc16e 2020-07-23 stsp } while (len > 0);
1156 af57b12a 2020-07-23 stsp
1157 af57b12a 2020-07-23 stsp (*s)[totlen] = '\0';
1158 af57b12a 2020-07-23 stsp return NULL;
1159 af57b12a 2020-07-23 stsp }
1160 af57b12a 2020-07-23 stsp
1161 af57b12a 2020-07-23 stsp const struct got_error *
1162 af57b12a 2020-07-23 stsp got_tree_entry_get_symlink_target(char **link_target, struct got_tree_entry *te,
1163 af57b12a 2020-07-23 stsp struct got_repository *repo)
1164 af57b12a 2020-07-23 stsp {
1165 af57b12a 2020-07-23 stsp const struct got_error *err = NULL;
1166 af57b12a 2020-07-23 stsp struct got_blob_object *blob = NULL;
1167 af57b12a 2020-07-23 stsp
1168 af57b12a 2020-07-23 stsp *link_target = NULL;
1169 af57b12a 2020-07-23 stsp
1170 af57b12a 2020-07-23 stsp if (!got_object_tree_entry_is_symlink(te))
1171 af57b12a 2020-07-23 stsp return got_error(GOT_ERR_TREE_ENTRY_TYPE);
1172 af57b12a 2020-07-23 stsp
1173 af57b12a 2020-07-23 stsp err = got_object_open_as_blob(&blob, repo,
1174 af57b12a 2020-07-23 stsp got_tree_entry_get_id(te), PATH_MAX);
1175 af57b12a 2020-07-23 stsp if (err)
1176 af57b12a 2020-07-23 stsp return err;
1177 af57b12a 2020-07-23 stsp
1178 af57b12a 2020-07-23 stsp err = got_object_blob_read_to_str(link_target, blob);
1179 af57b12a 2020-07-23 stsp got_object_blob_close(blob);
1180 659dc16e 2020-07-23 stsp if (err) {
1181 659dc16e 2020-07-23 stsp free(*link_target);
1182 659dc16e 2020-07-23 stsp *link_target = NULL;
1183 659dc16e 2020-07-23 stsp }
1184 0d6c6ee3 2020-05-20 stsp return err;
1185 56e0773d 2019-11-28 stsp }
1186 56e0773d 2019-11-28 stsp
1187 56e0773d 2019-11-28 stsp int
1188 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
1189 56e0773d 2019-11-28 stsp {
1190 56e0773d 2019-11-28 stsp return te->idx;
1191 56e0773d 2019-11-28 stsp }
1192 56e0773d 2019-11-28 stsp
1193 56e0773d 2019-11-28 stsp struct got_tree_entry *
1194 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
1195 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1196 56e0773d 2019-11-28 stsp {
1197 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
1198 56e0773d 2019-11-28 stsp }
1199 56e0773d 2019-11-28 stsp
1200 56e0773d 2019-11-28 stsp struct got_tree_entry *
1201 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
1202 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
1203 56e0773d 2019-11-28 stsp {
1204 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
1205 56e0773d 2019-11-28 stsp }
1206 56e0773d 2019-11-28 stsp
1207 3840f4c9 2018-09-12 stsp static const struct got_error *
1208 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1209 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1210 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1211 3840f4c9 2018-09-12 stsp {
1212 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
1213 3840f4c9 2018-09-12 stsp int outfd_child;
1214 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
1215 24140570 2018-09-09 stsp
1216 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
1217 3840f4c9 2018-09-12 stsp if (basefd == -1)
1218 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1219 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
1220 3840f4c9 2018-09-12 stsp if (accumfd == -1)
1221 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1222 3840f4c9 2018-09-12 stsp
1223 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
1224 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
1225 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1226 3840f4c9 2018-09-12 stsp
1227 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
1228 3840f4c9 2018-09-12 stsp if (err)
1229 3840f4c9 2018-09-12 stsp return err;
1230 3840f4c9 2018-09-12 stsp
1231 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
1232 3840f4c9 2018-09-12 stsp outfd_child);
1233 3840f4c9 2018-09-12 stsp if (err) {
1234 41496140 2019-02-21 stsp close(basefd);
1235 41496140 2019-02-21 stsp close(accumfd);
1236 3840f4c9 2018-09-12 stsp return err;
1237 3840f4c9 2018-09-12 stsp }
1238 41496140 2019-02-21 stsp
1239 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1240 3840f4c9 2018-09-12 stsp basefd);
1241 3840f4c9 2018-09-12 stsp if (err) {
1242 3840f4c9 2018-09-12 stsp close(accumfd);
1243 3840f4c9 2018-09-12 stsp return err;
1244 3840f4c9 2018-09-12 stsp }
1245 3840f4c9 2018-09-12 stsp
1246 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
1247 3840f4c9 2018-09-12 stsp accumfd);
1248 41496140 2019-02-21 stsp if (err)
1249 3840f4c9 2018-09-12 stsp return err;
1250 3840f4c9 2018-09-12 stsp
1251 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
1252 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
1253 3840f4c9 2018-09-12 stsp if (err)
1254 3840f4c9 2018-09-12 stsp return err;
1255 3840f4c9 2018-09-12 stsp
1256 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1257 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1258 3840f4c9 2018-09-12 stsp
1259 3840f4c9 2018-09-12 stsp return err;
1260 3840f4c9 2018-09-12 stsp }
1261 3840f4c9 2018-09-12 stsp
1262 ebc55e2d 2018-12-24 stsp static const struct got_error *
1263 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1264 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
1265 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
1266 68482ea3 2017-11-27 stsp {
1267 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
1268 ebc55e2d 2018-12-24 stsp
1269 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
1270 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1271 ebc55e2d 2018-12-24 stsp if (err)
1272 ebc55e2d 2018-12-24 stsp return err;
1273 ebc55e2d 2018-12-24 stsp }
1274 68482ea3 2017-11-27 stsp
1275 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
1276 ac544f8c 2019-01-13 stsp idx, id);
1277 9f2369b0 2018-12-24 stsp }
1278 9f2369b0 2018-12-24 stsp
1279 9f2369b0 2018-12-24 stsp static const struct got_error *
1280 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
1281 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
1282 9f2369b0 2018-12-24 stsp {
1283 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1284 9f2369b0 2018-12-24 stsp int outfd_child;
1285 9f2369b0 2018-12-24 stsp
1286 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
1287 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
1288 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
1289 9f2369b0 2018-12-24 stsp
1290 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
1291 9f2369b0 2018-12-24 stsp if (err)
1292 9f2369b0 2018-12-24 stsp return err;
1293 9f2369b0 2018-12-24 stsp
1294 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
1295 41496140 2019-02-21 stsp if (err)
1296 9f2369b0 2018-12-24 stsp return err;
1297 9f2369b0 2018-12-24 stsp
1298 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
1299 9f2369b0 2018-12-24 stsp if (err)
1300 9f2369b0 2018-12-24 stsp return err;
1301 9f2369b0 2018-12-24 stsp
1302 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
1303 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
1304 9f2369b0 2018-12-24 stsp
1305 9f2369b0 2018-12-24 stsp return err;
1306 9f2369b0 2018-12-24 stsp }
1307 9f2369b0 2018-12-24 stsp
1308 9f2369b0 2018-12-24 stsp static const struct got_error *
1309 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
1310 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
1311 9f2369b0 2018-12-24 stsp {
1312 ddc7b220 2019-09-08 stsp const struct got_error *err;
1313 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1314 9f2369b0 2018-12-24 stsp pid_t pid;
1315 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1316 9f2369b0 2018-12-24 stsp
1317 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
1318 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
1319 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1320 9f2369b0 2018-12-24 stsp }
1321 9f2369b0 2018-12-24 stsp
1322 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1323 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1324 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1325 9f2369b0 2018-12-24 stsp
1326 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1327 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1328 ddc7b220 2019-09-08 stsp free(ibuf);
1329 ddc7b220 2019-09-08 stsp return err;
1330 ddc7b220 2019-09-08 stsp }
1331 9f2369b0 2018-12-24 stsp
1332 9f2369b0 2018-12-24 stsp pid = fork();
1333 ddc7b220 2019-09-08 stsp if (pid == -1) {
1334 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1335 ddc7b220 2019-09-08 stsp free(ibuf);
1336 ddc7b220 2019-09-08 stsp return err;
1337 ddc7b220 2019-09-08 stsp }
1338 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1339 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1340 9f2369b0 2018-12-24 stsp repo->path);
1341 9f2369b0 2018-12-24 stsp /* not reached */
1342 9f2369b0 2018-12-24 stsp }
1343 9f2369b0 2018-12-24 stsp
1344 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1345 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1346 ddc7b220 2019-09-08 stsp free(ibuf);
1347 ddc7b220 2019-09-08 stsp return err;
1348 ddc7b220 2019-09-08 stsp }
1349 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1350 9f2369b0 2018-12-24 stsp imsg_fds[0];
1351 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1352 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1353 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1354 9f2369b0 2018-12-24 stsp
1355 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1356 ebc55e2d 2018-12-24 stsp }
1357 68482ea3 2017-11-27 stsp
1358 ebc55e2d 2018-12-24 stsp static const struct got_error *
1359 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1360 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1361 ebc55e2d 2018-12-24 stsp {
1362 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1363 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1364 ebc55e2d 2018-12-24 stsp int idx;
1365 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1366 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1367 e82b1d81 2019-07-27 stsp int outfd;
1368 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1369 ebc55e2d 2018-12-24 stsp struct stat sb;
1370 ebc55e2d 2018-12-24 stsp
1371 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1372 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1373 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1374 68482ea3 2017-11-27 stsp
1375 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1376 55da3778 2018-09-10 stsp if (outfd == -1)
1377 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1378 55da3778 2018-09-10 stsp
1379 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1380 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1381 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1382 c7254d79 2018-04-24 stsp goto done;
1383 15c8b0e6 2018-04-24 stsp }
1384 ebc55e2d 2018-12-24 stsp
1385 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1386 e82b1d81 2019-07-27 stsp if (err == NULL) {
1387 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1388 34f480ff 2019-07-27 stsp
1389 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1390 ebc55e2d 2018-12-24 stsp if (err)
1391 ebc55e2d 2018-12-24 stsp goto done;
1392 ebc55e2d 2018-12-24 stsp
1393 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1394 55da3778 2018-09-10 stsp if (pack == NULL) {
1395 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1396 ebc55e2d 2018-12-24 stsp packidx);
1397 55da3778 2018-09-10 stsp if (err)
1398 55da3778 2018-09-10 stsp goto done;
1399 55da3778 2018-09-10 stsp }
1400 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1401 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1402 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1403 e82b1d81 2019-07-27 stsp int infd;
1404 e82b1d81 2019-07-27 stsp
1405 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&infd, id, repo);
1406 e82b1d81 2019-07-27 stsp if (err)
1407 e82b1d81 2019-07-27 stsp goto done;
1408 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1409 ac544f8c 2019-01-13 stsp repo);
1410 e82b1d81 2019-07-27 stsp }
1411 ebc55e2d 2018-12-24 stsp if (err)
1412 55da3778 2018-09-10 stsp goto done;
1413 2967a784 2018-04-24 stsp
1414 de060dff 2018-12-24 stsp if (hdrlen > size) {
1415 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1416 ebc55e2d 2018-12-24 stsp goto done;
1417 ebc55e2d 2018-12-24 stsp }
1418 ebc55e2d 2018-12-24 stsp
1419 ac544f8c 2019-01-13 stsp if (outbuf) {
1420 08578a35 2021-01-22 stsp if (close(outfd) == -1 && err == NULL)
1421 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1422 ac544f8c 2019-01-13 stsp outfd = -1;
1423 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1424 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1425 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1426 ac544f8c 2019-01-13 stsp free(outbuf);
1427 ac544f8c 2019-01-13 stsp goto done;
1428 ac544f8c 2019-01-13 stsp }
1429 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1430 ac544f8c 2019-01-13 stsp } else {
1431 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1432 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1433 ac544f8c 2019-01-13 stsp goto done;
1434 ac544f8c 2019-01-13 stsp }
1435 ac544f8c 2019-01-13 stsp
1436 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1437 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1438 ac544f8c 2019-01-13 stsp goto done;
1439 ac544f8c 2019-01-13 stsp }
1440 ac544f8c 2019-01-13 stsp
1441 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1442 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1443 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1444 ac544f8c 2019-01-13 stsp close(outfd);
1445 ac544f8c 2019-01-13 stsp outfd = -1;
1446 ac544f8c 2019-01-13 stsp goto done;
1447 ac544f8c 2019-01-13 stsp }
1448 68482ea3 2017-11-27 stsp }
1449 68482ea3 2017-11-27 stsp
1450 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1451 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1452 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1453 7d283eee 2017-11-29 stsp
1454 c7254d79 2018-04-24 stsp done:
1455 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1456 55da3778 2018-09-10 stsp if (err) {
1457 55da3778 2018-09-10 stsp if (*blob) {
1458 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1459 55da3778 2018-09-10 stsp *blob = NULL;
1460 55da3778 2018-09-10 stsp } else if (outfd != -1)
1461 55da3778 2018-09-10 stsp close(outfd);
1462 a19581a2 2018-06-21 stsp }
1463 a19581a2 2018-06-21 stsp return err;
1464 a19581a2 2018-06-21 stsp }
1465 a19581a2 2018-06-21 stsp
1466 a19581a2 2018-06-21 stsp const struct got_error *
1467 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1468 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1469 a19581a2 2018-06-21 stsp size_t blocksize)
1470 a19581a2 2018-06-21 stsp {
1471 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1472 ebc55e2d 2018-12-24 stsp }
1473 835e0dbd 2018-06-21 stsp
1474 ebc55e2d 2018-12-24 stsp const struct got_error *
1475 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1476 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1477 ebc55e2d 2018-12-24 stsp {
1478 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1479 0ffeb3c2 2017-11-26 stsp }
1480 68482ea3 2017-11-27 stsp
1481 fb43ecf1 2019-02-11 stsp const struct got_error *
1482 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1483 68482ea3 2017-11-27 stsp {
1484 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1485 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1486 56b63ca4 2021-01-22 stsp if (blob->f && fclose(blob->f) == EOF)
1487 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1488 ac544f8c 2019-01-13 stsp free(blob->data);
1489 68482ea3 2017-11-27 stsp free(blob);
1490 fb43ecf1 2019-02-11 stsp return err;
1491 f934cf2c 2018-02-12 stsp }
1492 f934cf2c 2018-02-12 stsp
1493 8ba819a3 2020-07-23 stsp void
1494 8ba819a3 2020-07-23 stsp got_object_blob_rewind(struct got_blob_object *blob)
1495 8ba819a3 2020-07-23 stsp {
1496 8ba819a3 2020-07-23 stsp if (blob->f)
1497 8ba819a3 2020-07-23 stsp rewind(blob->f);
1498 8ba819a3 2020-07-23 stsp }
1499 8ba819a3 2020-07-23 stsp
1500 f934cf2c 2018-02-12 stsp char *
1501 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1502 f934cf2c 2018-02-12 stsp {
1503 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1504 f934cf2c 2018-02-12 stsp }
1505 f934cf2c 2018-02-12 stsp
1506 f934cf2c 2018-02-12 stsp size_t
1507 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1508 f934cf2c 2018-02-12 stsp {
1509 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1510 68482ea3 2017-11-27 stsp }
1511 68482ea3 2017-11-27 stsp
1512 f934cf2c 2018-02-12 stsp const uint8_t *
1513 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1514 f934cf2c 2018-02-12 stsp {
1515 f934cf2c 2018-02-12 stsp return blob->read_buf;
1516 f934cf2c 2018-02-12 stsp }
1517 f934cf2c 2018-02-12 stsp
1518 68482ea3 2017-11-27 stsp const struct got_error *
1519 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1520 68482ea3 2017-11-27 stsp {
1521 eb651edf 2018-02-11 stsp size_t n;
1522 eb651edf 2018-02-11 stsp
1523 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1524 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1525 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1526 eb651edf 2018-02-11 stsp *outlenp = n;
1527 35e9ba5d 2018-06-21 stsp return NULL;
1528 35e9ba5d 2018-06-21 stsp }
1529 35e9ba5d 2018-06-21 stsp
1530 35e9ba5d 2018-06-21 stsp const struct got_error *
1531 be659d10 2020-11-18 stsp got_object_blob_dump_to_file(off_t *filesize, int *nlines,
1532 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1533 35e9ba5d 2018-06-21 stsp {
1534 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1535 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1536 84451b3e 2018-07-10 stsp const uint8_t *buf;
1537 84451b3e 2018-07-10 stsp int i;
1538 c33ebc60 2020-11-18 stsp const int alloc_chunksz = 512;
1539 c33ebc60 2020-11-18 stsp size_t nalloc = 0;
1540 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1541 84451b3e 2018-07-10 stsp
1542 6c4c42e0 2019-06-24 stsp if (line_offsets)
1543 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1544 f595d9bd 2019-08-14 stsp if (filesize)
1545 f595d9bd 2019-08-14 stsp *filesize = 0;
1546 84451b3e 2018-07-10 stsp if (nlines)
1547 84451b3e 2018-07-10 stsp *nlines = 0;
1548 35e9ba5d 2018-06-21 stsp
1549 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1550 35e9ba5d 2018-06-21 stsp do {
1551 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1552 35e9ba5d 2018-06-21 stsp if (err)
1553 35e9ba5d 2018-06-21 stsp return err;
1554 35e9ba5d 2018-06-21 stsp if (len == 0)
1555 35e9ba5d 2018-06-21 stsp break;
1556 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1557 b02560ec 2019-08-19 stsp i = hdrlen;
1558 f1cbc3bc 2020-11-18 stsp if (nlines) {
1559 f1cbc3bc 2020-11-18 stsp if (line_offsets && *line_offsets == NULL) {
1560 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1561 78695fb7 2019-08-12 stsp *nlines = 1;
1562 c33ebc60 2020-11-18 stsp nalloc = alloc_chunksz;
1563 c33ebc60 2020-11-18 stsp *line_offsets = calloc(nalloc,
1564 c33ebc60 2020-11-18 stsp sizeof(**line_offsets));
1565 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1566 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1567 b02560ec 2019-08-19 stsp
1568 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1569 b02560ec 2019-08-19 stsp while (i < len) {
1570 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1571 b02560ec 2019-08-19 stsp break;
1572 b02560ec 2019-08-19 stsp i++;
1573 b02560ec 2019-08-19 stsp }
1574 b02560ec 2019-08-19 stsp }
1575 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1576 b02560ec 2019-08-19 stsp while (i < len) {
1577 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1578 b02560ec 2019-08-19 stsp i++;
1579 f595d9bd 2019-08-14 stsp continue;
1580 b02560ec 2019-08-19 stsp }
1581 f595d9bd 2019-08-14 stsp (*nlines)++;
1582 c33ebc60 2020-11-18 stsp if (line_offsets && nalloc < *nlines) {
1583 c33ebc60 2020-11-18 stsp size_t n = *nlines + alloc_chunksz;
1584 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1585 c33ebc60 2020-11-18 stsp nalloc, n, sizeof(**line_offsets));
1586 78695fb7 2019-08-12 stsp if (o == NULL) {
1587 78695fb7 2019-08-12 stsp free(*line_offsets);
1588 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1589 78695fb7 2019-08-12 stsp return got_error_from_errno(
1590 78695fb7 2019-08-12 stsp "recallocarray");
1591 78695fb7 2019-08-12 stsp }
1592 78695fb7 2019-08-12 stsp *line_offsets = o;
1593 c33ebc60 2020-11-18 stsp nalloc = n;
1594 78695fb7 2019-08-12 stsp }
1595 f1cbc3bc 2020-11-18 stsp if (line_offsets) {
1596 f1cbc3bc 2020-11-18 stsp off = total_len + i - hdrlen + 1;
1597 f1cbc3bc 2020-11-18 stsp (*line_offsets)[*nlines - 1] = off;
1598 f1cbc3bc 2020-11-18 stsp }
1599 b02560ec 2019-08-19 stsp i++;
1600 6c4c42e0 2019-06-24 stsp }
1601 84451b3e 2018-07-10 stsp }
1602 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1603 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1604 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1605 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1606 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1607 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1608 35e9ba5d 2018-06-21 stsp } while (len != 0);
1609 35e9ba5d 2018-06-21 stsp
1610 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1611 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1612 35e9ba5d 2018-06-21 stsp rewind(outfile);
1613 35e9ba5d 2018-06-21 stsp
1614 f595d9bd 2019-08-14 stsp if (filesize)
1615 f595d9bd 2019-08-14 stsp *filesize = total_len;
1616 f595d9bd 2019-08-14 stsp
1617 776d4d29 2018-06-17 stsp return NULL;
1618 f4a881ce 2018-11-17 stsp }
1619 f4a881ce 2018-11-17 stsp
1620 f4a881ce 2018-11-17 stsp static const struct got_error *
1621 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1622 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1623 a158c901 2018-12-23 stsp {
1624 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1625 a158c901 2018-12-23 stsp
1626 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1627 268f7291 2018-12-24 stsp pack_idx);
1628 a158c901 2018-12-23 stsp if (err)
1629 a158c901 2018-12-23 stsp return err;
1630 a158c901 2018-12-23 stsp
1631 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1632 268f7291 2018-12-24 stsp }
1633 268f7291 2018-12-24 stsp
1634 268f7291 2018-12-24 stsp static const struct got_error *
1635 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1636 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1637 268f7291 2018-12-24 stsp struct got_object_id *id)
1638 268f7291 2018-12-24 stsp {
1639 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1640 268f7291 2018-12-24 stsp
1641 268f7291 2018-12-24 stsp if (pack->privsep_child)
1642 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1643 268f7291 2018-12-24 stsp
1644 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1645 268f7291 2018-12-24 stsp if (err)
1646 268f7291 2018-12-24 stsp return err;
1647 268f7291 2018-12-24 stsp
1648 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1649 a158c901 2018-12-23 stsp }
1650 9f2369b0 2018-12-24 stsp
1651 9f2369b0 2018-12-24 stsp static const struct got_error *
1652 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1653 9f2369b0 2018-12-24 stsp int fd)
1654 9f2369b0 2018-12-24 stsp {
1655 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1656 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1657 9f2369b0 2018-12-24 stsp
1658 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1659 9f2369b0 2018-12-24 stsp
1660 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1661 9f2369b0 2018-12-24 stsp if (err)
1662 9f2369b0 2018-12-24 stsp return err;
1663 9f2369b0 2018-12-24 stsp
1664 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1665 9f2369b0 2018-12-24 stsp }
1666 9f2369b0 2018-12-24 stsp
1667 9f2369b0 2018-12-24 stsp static const struct got_error *
1668 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1669 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1670 9f2369b0 2018-12-24 stsp {
1671 ddc7b220 2019-09-08 stsp const struct got_error *err;
1672 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1673 9f2369b0 2018-12-24 stsp pid_t pid;
1674 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1675 9f2369b0 2018-12-24 stsp
1676 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1677 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1678 9f2369b0 2018-12-24 stsp
1679 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1680 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1681 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1682 9f2369b0 2018-12-24 stsp
1683 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1684 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1685 ddc7b220 2019-09-08 stsp free(ibuf);
1686 ddc7b220 2019-09-08 stsp return err;
1687 ddc7b220 2019-09-08 stsp }
1688 9f2369b0 2018-12-24 stsp
1689 9f2369b0 2018-12-24 stsp pid = fork();
1690 ddc7b220 2019-09-08 stsp if (pid == -1) {
1691 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1692 ddc7b220 2019-09-08 stsp free(ibuf);
1693 ddc7b220 2019-09-08 stsp return err;
1694 ddc7b220 2019-09-08 stsp }
1695 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1696 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1697 9f2369b0 2018-12-24 stsp repo->path);
1698 9f2369b0 2018-12-24 stsp /* not reached */
1699 9f2369b0 2018-12-24 stsp }
1700 9f2369b0 2018-12-24 stsp
1701 08578a35 2021-01-22 stsp if (close(imsg_fds[1]) == -1) {
1702 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1703 ddc7b220 2019-09-08 stsp free(ibuf);
1704 ddc7b220 2019-09-08 stsp return err;
1705 ddc7b220 2019-09-08 stsp }
1706 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1707 9f2369b0 2018-12-24 stsp imsg_fds[0];
1708 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1709 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1710 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1711 a158c901 2018-12-23 stsp
1712 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1713 9f2369b0 2018-12-24 stsp }
1714 a158c901 2018-12-23 stsp
1715 a158c901 2018-12-23 stsp static const struct got_error *
1716 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1717 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1718 f4a881ce 2018-11-17 stsp {
1719 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1720 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1721 e82b1d81 2019-07-27 stsp int idx;
1722 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1723 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1724 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1725 f4a881ce 2018-11-17 stsp
1726 f4a881ce 2018-11-17 stsp if (check_cache) {
1727 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1728 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1729 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1730 f4a881ce 2018-11-17 stsp return NULL;
1731 f4a881ce 2018-11-17 stsp }
1732 f4a881ce 2018-11-17 stsp } else
1733 f4a881ce 2018-11-17 stsp *tag = NULL;
1734 f4a881ce 2018-11-17 stsp
1735 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1736 e82b1d81 2019-07-27 stsp if (err == NULL) {
1737 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1738 f4a881ce 2018-11-17 stsp
1739 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
1740 268f7291 2018-12-24 stsp if (err)
1741 268f7291 2018-12-24 stsp return err;
1742 268f7291 2018-12-24 stsp
1743 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1744 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1745 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1746 e82b1d81 2019-07-27 stsp packidx);
1747 f4a881ce 2018-11-17 stsp if (err)
1748 8d2c5ea3 2019-08-13 stsp goto done;
1749 f4a881ce 2018-11-17 stsp }
1750 5d844a1e 2019-08-13 stsp
1751 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1752 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1753 5d844a1e 2019-08-13 stsp idx, id);
1754 5d844a1e 2019-08-13 stsp if (err)
1755 5d844a1e 2019-08-13 stsp goto done;
1756 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1757 5d844a1e 2019-08-13 stsp got_object_close(obj);
1758 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1759 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1760 5d844a1e 2019-08-13 stsp goto done;
1761 5d844a1e 2019-08-13 stsp }
1762 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1763 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1764 e82b1d81 2019-07-27 stsp int fd;
1765 e82b1d81 2019-07-27 stsp
1766 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1767 e82b1d81 2019-07-27 stsp if (err)
1768 e82b1d81 2019-07-27 stsp return err;
1769 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1770 5d844a1e 2019-08-13 stsp if (err)
1771 5d844a1e 2019-08-13 stsp return err;
1772 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1773 5d844a1e 2019-08-13 stsp got_object_close(obj);
1774 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1775 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1776 5d844a1e 2019-08-13 stsp
1777 762d73f4 2021-04-10 stsp err = got_object_open_loose_fd(&fd, id, repo);
1778 5d844a1e 2019-08-13 stsp if (err)
1779 5d844a1e 2019-08-13 stsp return err;
1780 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1781 e82b1d81 2019-07-27 stsp }
1782 f4a881ce 2018-11-17 stsp
1783 f4a881ce 2018-11-17 stsp if (err == NULL) {
1784 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1785 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1786 f4a881ce 2018-11-17 stsp }
1787 8d2c5ea3 2019-08-13 stsp done:
1788 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1789 f4a881ce 2018-11-17 stsp return err;
1790 f4a881ce 2018-11-17 stsp }
1791 f4a881ce 2018-11-17 stsp
1792 f4a881ce 2018-11-17 stsp const struct got_error *
1793 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1794 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1795 f4a881ce 2018-11-17 stsp {
1796 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1797 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1798 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1799 f4a881ce 2018-11-17 stsp return NULL;
1800 f4a881ce 2018-11-17 stsp }
1801 f4a881ce 2018-11-17 stsp
1802 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1803 f4a881ce 2018-11-17 stsp }
1804 f4a881ce 2018-11-17 stsp
1805 f4a881ce 2018-11-17 stsp const struct got_error *
1806 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1807 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1808 f4a881ce 2018-11-17 stsp {
1809 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1810 d24820bf 2019-08-11 stsp }
1811 d24820bf 2019-08-11 stsp
1812 d24820bf 2019-08-11 stsp const char *
1813 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1814 d24820bf 2019-08-11 stsp {
1815 d24820bf 2019-08-11 stsp return tag->tag;
1816 0bd18d37 2019-02-01 stsp }
1817 0bd18d37 2019-02-01 stsp
1818 0bd18d37 2019-02-01 stsp int
1819 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1820 0bd18d37 2019-02-01 stsp {
1821 0bd18d37 2019-02-01 stsp return tag->obj_type;
1822 0bd18d37 2019-02-01 stsp }
1823 0bd18d37 2019-02-01 stsp
1824 0bd18d37 2019-02-01 stsp struct got_object_id *
1825 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1826 0bd18d37 2019-02-01 stsp {
1827 0bd18d37 2019-02-01 stsp return &tag->id;
1828 01073a5d 2019-08-22 stsp }
1829 01073a5d 2019-08-22 stsp
1830 01073a5d 2019-08-22 stsp time_t
1831 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1832 01073a5d 2019-08-22 stsp {
1833 01073a5d 2019-08-22 stsp return tag->tagger_time;
1834 01073a5d 2019-08-22 stsp }
1835 01073a5d 2019-08-22 stsp
1836 01073a5d 2019-08-22 stsp time_t
1837 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1838 01073a5d 2019-08-22 stsp {
1839 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1840 01073a5d 2019-08-22 stsp }
1841 01073a5d 2019-08-22 stsp
1842 01073a5d 2019-08-22 stsp const char *
1843 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1844 01073a5d 2019-08-22 stsp {
1845 01073a5d 2019-08-22 stsp return tag->tagger;
1846 776d4d29 2018-06-17 stsp }
1847 776d4d29 2018-06-17 stsp
1848 01073a5d 2019-08-22 stsp const char *
1849 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1850 01073a5d 2019-08-22 stsp {
1851 01073a5d 2019-08-22 stsp return tag->tagmsg;
1852 01073a5d 2019-08-22 stsp }
1853 01073a5d 2019-08-22 stsp
1854 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1855 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1856 776d4d29 2018-06-17 stsp {
1857 56e0773d 2019-11-28 stsp int i;
1858 776d4d29 2018-06-17 stsp
1859 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1860 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1861 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1862 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1863 63da309a 2018-11-07 stsp if (cmp < 0)
1864 63da309a 2018-11-07 stsp continue;
1865 63da309a 2018-11-07 stsp if (cmp > 0)
1866 63da309a 2018-11-07 stsp break;
1867 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1868 776d4d29 2018-06-17 stsp return te;
1869 776d4d29 2018-06-17 stsp }
1870 eb651edf 2018-02-11 stsp return NULL;
1871 a129376b 2019-03-28 stsp }
1872 a129376b 2019-03-28 stsp
1873 56e0773d 2019-11-28 stsp struct got_tree_entry *
1874 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1875 a129376b 2019-03-28 stsp {
1876 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1877 776d4d29 2018-06-17 stsp }
1878 776d4d29 2018-06-17 stsp
1879 776d4d29 2018-06-17 stsp const struct got_error *
1880 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1881 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1882 776d4d29 2018-06-17 stsp {
1883 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1884 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1885 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1886 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1887 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1888 b7cd37e5 2018-11-18 stsp size_t seglen;
1889 776d4d29 2018-06-17 stsp
1890 27d434c2 2018-09-15 stsp *id = NULL;
1891 776d4d29 2018-06-17 stsp
1892 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1893 776d4d29 2018-06-17 stsp if (err)
1894 776d4d29 2018-06-17 stsp goto done;
1895 776d4d29 2018-06-17 stsp
1896 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1897 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1898 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1899 27d434c2 2018-09-15 stsp if (*id == NULL)
1900 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1901 ca008b32 2018-07-22 stsp goto done;
1902 776d4d29 2018-06-17 stsp }
1903 776d4d29 2018-06-17 stsp
1904 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1905 776d4d29 2018-06-17 stsp if (err)
1906 776d4d29 2018-06-17 stsp goto done;
1907 776d4d29 2018-06-17 stsp
1908 65a9bbe9 2018-09-15 stsp s = path;
1909 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1910 5e54fb30 2019-05-31 stsp s++;
1911 776d4d29 2018-06-17 stsp seg = s;
1912 65a9bbe9 2018-09-15 stsp seglen = 0;
1913 b7cd37e5 2018-11-18 stsp while (*s) {
1914 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1915 776d4d29 2018-06-17 stsp
1916 776d4d29 2018-06-17 stsp if (*s != '/') {
1917 776d4d29 2018-06-17 stsp s++;
1918 65a9bbe9 2018-09-15 stsp seglen++;
1919 00530cfb 2018-06-21 stsp if (*s)
1920 00530cfb 2018-06-21 stsp continue;
1921 776d4d29 2018-06-17 stsp }
1922 776d4d29 2018-06-17 stsp
1923 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1924 db37e2c0 2018-06-21 stsp if (te == NULL) {
1925 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1926 776d4d29 2018-06-17 stsp goto done;
1927 776d4d29 2018-06-17 stsp }
1928 776d4d29 2018-06-17 stsp
1929 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1930 67606321 2018-06-21 stsp break;
1931 67606321 2018-06-21 stsp
1932 776d4d29 2018-06-17 stsp seg = s + 1;
1933 65a9bbe9 2018-09-15 stsp seglen = 0;
1934 776d4d29 2018-06-17 stsp s++;
1935 776d4d29 2018-06-17 stsp if (*s) {
1936 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1937 56e0773d 2019-11-28 stsp &te->id);
1938 db37e2c0 2018-06-21 stsp te = NULL;
1939 776d4d29 2018-06-17 stsp if (err)
1940 776d4d29 2018-06-17 stsp goto done;
1941 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1942 776d4d29 2018-06-17 stsp tree = next_tree;
1943 776d4d29 2018-06-17 stsp }
1944 776d4d29 2018-06-17 stsp }
1945 776d4d29 2018-06-17 stsp
1946 27d434c2 2018-09-15 stsp if (te) {
1947 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1948 27d434c2 2018-09-15 stsp if (*id == NULL)
1949 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1950 27d434c2 2018-09-15 stsp } else
1951 b66cd6f3 2020-07-31 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
1952 776d4d29 2018-06-17 stsp done:
1953 776d4d29 2018-06-17 stsp if (commit)
1954 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1955 776d4d29 2018-06-17 stsp if (tree)
1956 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1957 776d4d29 2018-06-17 stsp return err;
1958 ac5f2b26 2020-05-05 stsp }
1959 ac5f2b26 2020-05-05 stsp
1960 ac5f2b26 2020-05-05 stsp /*
1961 ac5f2b26 2020-05-05 stsp * Normalize file mode bits to avoid false positive tree entry differences
1962 ac5f2b26 2020-05-05 stsp * in case tree entries have unexpected mode bits set.
1963 ac5f2b26 2020-05-05 stsp */
1964 ac5f2b26 2020-05-05 stsp static mode_t
1965 ac5f2b26 2020-05-05 stsp normalize_mode_for_comparison(mode_t mode)
1966 ac5f2b26 2020-05-05 stsp {
1967 ac5f2b26 2020-05-05 stsp /*
1968 ac5f2b26 2020-05-05 stsp * For directories, the only relevant bit is the IFDIR bit.
1969 ac5f2b26 2020-05-05 stsp * This allows us to detect paths changing from a directory
1970 ac5f2b26 2020-05-05 stsp * to a file and vice versa.
1971 ac5f2b26 2020-05-05 stsp */
1972 ac5f2b26 2020-05-05 stsp if (S_ISDIR(mode))
1973 ac5f2b26 2020-05-05 stsp return mode & S_IFDIR;
1974 40dde666 2020-07-23 stsp
1975 40dde666 2020-07-23 stsp /*
1976 40dde666 2020-07-23 stsp * For symlinks, the only relevant bit is the IFLNK bit.
1977 40dde666 2020-07-23 stsp * This allows us to detect paths changing from a symlinks
1978 40dde666 2020-07-23 stsp * to a file or directory and vice versa.
1979 40dde666 2020-07-23 stsp */
1980 40dde666 2020-07-23 stsp if (S_ISLNK(mode))
1981 40dde666 2020-07-23 stsp return mode & S_IFLNK;
1982 ac5f2b26 2020-05-05 stsp
1983 ac5f2b26 2020-05-05 stsp /* For files, the only change we care about is the executable bit. */
1984 ac5f2b26 2020-05-05 stsp return mode & S_IXUSR;
1985 68482ea3 2017-11-27 stsp }
1986 07862c20 2018-09-15 stsp
1987 07862c20 2018-09-15 stsp const struct got_error *
1988 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1989 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1990 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1991 07862c20 2018-09-15 stsp {
1992 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1993 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1994 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1995 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1996 3b7f9878 2018-11-18 stsp size_t seglen;
1997 07862c20 2018-09-15 stsp
1998 07862c20 2018-09-15 stsp *changed = 0;
1999 07862c20 2018-09-15 stsp
2000 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
2001 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
2002 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
2003 07862c20 2018-09-15 stsp
2004 07862c20 2018-09-15 stsp tree1 = tree01;
2005 07862c20 2018-09-15 stsp tree2 = tree02;
2006 65a9bbe9 2018-09-15 stsp s = path;
2007 61a7d79f 2020-02-29 stsp while (*s == '/')
2008 61a7d79f 2020-02-29 stsp s++;
2009 07862c20 2018-09-15 stsp seg = s;
2010 65a9bbe9 2018-09-15 stsp seglen = 0;
2011 3b7f9878 2018-11-18 stsp while (*s) {
2012 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
2013 ac5f2b26 2020-05-05 stsp mode_t mode1, mode2;
2014 07862c20 2018-09-15 stsp
2015 07862c20 2018-09-15 stsp if (*s != '/') {
2016 07862c20 2018-09-15 stsp s++;
2017 65a9bbe9 2018-09-15 stsp seglen++;
2018 07862c20 2018-09-15 stsp if (*s)
2019 07862c20 2018-09-15 stsp continue;
2020 07862c20 2018-09-15 stsp }
2021 07862c20 2018-09-15 stsp
2022 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
2023 07862c20 2018-09-15 stsp if (te1 == NULL) {
2024 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
2025 07862c20 2018-09-15 stsp goto done;
2026 07862c20 2018-09-15 stsp }
2027 07862c20 2018-09-15 stsp
2028 e8bfb8f3 2020-12-18 stsp if (tree2)
2029 e8bfb8f3 2020-12-18 stsp te2 = find_entry_by_name(tree2, seg, seglen);
2030 07862c20 2018-09-15 stsp
2031 e8bfb8f3 2020-12-18 stsp if (te2) {
2032 e8bfb8f3 2020-12-18 stsp mode1 = normalize_mode_for_comparison(te1->mode);
2033 e8bfb8f3 2020-12-18 stsp mode2 = normalize_mode_for_comparison(te2->mode);
2034 e8bfb8f3 2020-12-18 stsp if (mode1 != mode2) {
2035 e8bfb8f3 2020-12-18 stsp *changed = 1;
2036 e8bfb8f3 2020-12-18 stsp goto done;
2037 e8bfb8f3 2020-12-18 stsp }
2038 e8bfb8f3 2020-12-18 stsp
2039 e8bfb8f3 2020-12-18 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
2040 e8bfb8f3 2020-12-18 stsp *changed = 0;
2041 e8bfb8f3 2020-12-18 stsp goto done;
2042 e8bfb8f3 2020-12-18 stsp }
2043 07862c20 2018-09-15 stsp }
2044 07862c20 2018-09-15 stsp
2045 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
2046 07862c20 2018-09-15 stsp *changed = 1;
2047 07862c20 2018-09-15 stsp goto done;
2048 07862c20 2018-09-15 stsp }
2049 07862c20 2018-09-15 stsp
2050 07862c20 2018-09-15 stsp seg = s + 1;
2051 07862c20 2018-09-15 stsp s++;
2052 65a9bbe9 2018-09-15 stsp seglen = 0;
2053 07862c20 2018-09-15 stsp if (*s) {
2054 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
2055 56e0773d 2019-11-28 stsp &te1->id);
2056 07862c20 2018-09-15 stsp te1 = NULL;
2057 07862c20 2018-09-15 stsp if (err)
2058 07862c20 2018-09-15 stsp goto done;
2059 a31cea73 2018-09-15 stsp if (tree1 != tree01)
2060 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
2061 07862c20 2018-09-15 stsp tree1 = next_tree1;
2062 07862c20 2018-09-15 stsp
2063 e8bfb8f3 2020-12-18 stsp if (te2) {
2064 e8bfb8f3 2020-12-18 stsp err = got_object_open_as_tree(&next_tree2, repo,
2065 e8bfb8f3 2020-12-18 stsp &te2->id);
2066 e8bfb8f3 2020-12-18 stsp te2 = NULL;
2067 e8bfb8f3 2020-12-18 stsp if (err)
2068 e8bfb8f3 2020-12-18 stsp goto done;
2069 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2070 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2071 e8bfb8f3 2020-12-18 stsp tree2 = next_tree2;
2072 e8bfb8f3 2020-12-18 stsp } else if (tree2) {
2073 e8bfb8f3 2020-12-18 stsp if (tree2 != tree02)
2074 e8bfb8f3 2020-12-18 stsp got_object_tree_close(tree2);
2075 e8bfb8f3 2020-12-18 stsp tree2 = NULL;
2076 e8bfb8f3 2020-12-18 stsp }
2077 07862c20 2018-09-15 stsp }
2078 07862c20 2018-09-15 stsp }
2079 07862c20 2018-09-15 stsp done:
2080 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
2081 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
2082 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
2083 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
2084 77880158 2018-11-04 stsp return err;
2085 77880158 2018-11-04 stsp }
2086 ed175427 2019-05-09 stsp
2087 ed175427 2019-05-09 stsp const struct got_error *
2088 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
2089 ed175427 2019-05-09 stsp struct got_tree_entry *te)
2090 ed175427 2019-05-09 stsp {
2091 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
2092 ed175427 2019-05-09 stsp
2093 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
2094 ed175427 2019-05-09 stsp if (*new_te == NULL)
2095 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
2096 ed175427 2019-05-09 stsp
2097 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
2098 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
2099 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
2100 8c4eabf2 2019-05-10 stsp return err;
2101 63c5ca5d 2019-08-24 stsp }
2102 63c5ca5d 2019-08-24 stsp
2103 63c5ca5d 2019-08-24 stsp int
2104 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
2105 63c5ca5d 2019-08-24 stsp {
2106 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
2107 e40622f4 2020-07-23 stsp }
2108 e40622f4 2020-07-23 stsp
2109 e40622f4 2020-07-23 stsp int
2110 e40622f4 2020-07-23 stsp got_object_tree_entry_is_symlink(struct got_tree_entry *te)
2111 e40622f4 2020-07-23 stsp {
2112 e40622f4 2020-07-23 stsp /* S_IFDIR check avoids confusing symlinks with submodules. */
2113 e40622f4 2020-07-23 stsp return ((te->mode & (S_IFDIR | S_IFLNK)) == S_IFLNK);
2114 e40622f4 2020-07-23 stsp }
2115 e40622f4 2020-07-23 stsp
2116 e40622f4 2020-07-23 stsp static const struct got_error *
2117 e40622f4 2020-07-23 stsp resolve_symlink(char **link_target, const char *path,
2118 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2119 e40622f4 2020-07-23 stsp {
2120 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2121 dbdd6209 2020-10-19 stsp char buf[PATH_MAX];
2122 e40622f4 2020-07-23 stsp char *name, *parent_path = NULL;
2123 e40622f4 2020-07-23 stsp struct got_object_id *tree_obj_id = NULL;
2124 e40622f4 2020-07-23 stsp struct got_tree_object *tree = NULL;
2125 e40622f4 2020-07-23 stsp struct got_tree_entry *te = NULL;
2126 e40622f4 2020-07-23 stsp
2127 e40622f4 2020-07-23 stsp *link_target = NULL;
2128 559d127c 2020-07-23 stsp
2129 dbdd6209 2020-10-19 stsp if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf))
2130 dbdd6209 2020-10-19 stsp return got_error(GOT_ERR_NO_SPACE);
2131 dbdd6209 2020-10-19 stsp
2132 dbdd6209 2020-10-19 stsp name = basename(buf);
2133 e40622f4 2020-07-23 stsp if (name == NULL)
2134 e40622f4 2020-07-23 stsp return got_error_from_errno2("basename", path);
2135 e40622f4 2020-07-23 stsp
2136 e40622f4 2020-07-23 stsp err = got_path_dirname(&parent_path, path);
2137 e40622f4 2020-07-23 stsp if (err)
2138 e40622f4 2020-07-23 stsp return err;
2139 e40622f4 2020-07-23 stsp
2140 e40622f4 2020-07-23 stsp err = got_object_id_by_path(&tree_obj_id, repo, commit_id,
2141 e40622f4 2020-07-23 stsp parent_path);
2142 e40622f4 2020-07-23 stsp if (err) {
2143 e40622f4 2020-07-23 stsp if (err->code == GOT_ERR_NO_TREE_ENTRY) {
2144 e40622f4 2020-07-23 stsp /* Display the complete path in error message. */
2145 e40622f4 2020-07-23 stsp err = got_error_path(path, err->code);
2146 e40622f4 2020-07-23 stsp }
2147 e40622f4 2020-07-23 stsp goto done;
2148 e40622f4 2020-07-23 stsp }
2149 e40622f4 2020-07-23 stsp
2150 e40622f4 2020-07-23 stsp err = got_object_open_as_tree(&tree, repo, tree_obj_id);
2151 e40622f4 2020-07-23 stsp if (err)
2152 e40622f4 2020-07-23 stsp goto done;
2153 e40622f4 2020-07-23 stsp
2154 e40622f4 2020-07-23 stsp te = got_object_tree_find_entry(tree, name);
2155 e40622f4 2020-07-23 stsp if (te == NULL) {
2156 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_NO_TREE_ENTRY);
2157 e40622f4 2020-07-23 stsp goto done;
2158 e40622f4 2020-07-23 stsp }
2159 e40622f4 2020-07-23 stsp
2160 e40622f4 2020-07-23 stsp if (got_object_tree_entry_is_symlink(te)) {
2161 e40622f4 2020-07-23 stsp err = got_tree_entry_get_symlink_target(link_target, te, repo);
2162 e40622f4 2020-07-23 stsp if (err)
2163 e40622f4 2020-07-23 stsp goto done;
2164 e40622f4 2020-07-23 stsp if (!got_path_is_absolute(*link_target)) {
2165 e40622f4 2020-07-23 stsp char *abspath;
2166 e40622f4 2020-07-23 stsp if (asprintf(&abspath, "%s/%s", parent_path,
2167 e40622f4 2020-07-23 stsp *link_target) == -1) {
2168 e40622f4 2020-07-23 stsp err = got_error_from_errno("asprintf");
2169 e40622f4 2020-07-23 stsp goto done;
2170 e40622f4 2020-07-23 stsp }
2171 e40622f4 2020-07-23 stsp free(*link_target);
2172 e40622f4 2020-07-23 stsp *link_target = malloc(PATH_MAX);
2173 e40622f4 2020-07-23 stsp if (*link_target == NULL) {
2174 e40622f4 2020-07-23 stsp err = got_error_from_errno("malloc");
2175 e40622f4 2020-07-23 stsp goto done;
2176 e40622f4 2020-07-23 stsp }
2177 e40622f4 2020-07-23 stsp err = got_canonpath(abspath, *link_target, PATH_MAX);
2178 e40622f4 2020-07-23 stsp free(abspath);
2179 e40622f4 2020-07-23 stsp if (err)
2180 e40622f4 2020-07-23 stsp goto done;
2181 e40622f4 2020-07-23 stsp }
2182 e40622f4 2020-07-23 stsp }
2183 e40622f4 2020-07-23 stsp done:
2184 e40622f4 2020-07-23 stsp free(tree_obj_id);
2185 e40622f4 2020-07-23 stsp if (tree)
2186 e40622f4 2020-07-23 stsp got_object_tree_close(tree);
2187 e40622f4 2020-07-23 stsp if (err) {
2188 e40622f4 2020-07-23 stsp free(*link_target);
2189 e40622f4 2020-07-23 stsp *link_target = NULL;
2190 e40622f4 2020-07-23 stsp }
2191 e40622f4 2020-07-23 stsp return err;
2192 ca6e02ac 2020-01-07 stsp }
2193 ca6e02ac 2020-01-07 stsp
2194 ca6e02ac 2020-01-07 stsp const struct got_error *
2195 e40622f4 2020-07-23 stsp got_object_resolve_symlinks(char **link_target, const char *path,
2196 e40622f4 2020-07-23 stsp struct got_object_id *commit_id, struct got_repository *repo)
2197 e40622f4 2020-07-23 stsp {
2198 e40622f4 2020-07-23 stsp const struct got_error *err = NULL;
2199 e40622f4 2020-07-23 stsp char *next_target = NULL;
2200 e40622f4 2020-07-23 stsp int max_recursion = 40; /* matches Git */
2201 e40622f4 2020-07-23 stsp
2202 e40622f4 2020-07-23 stsp *link_target = NULL;
2203 e40622f4 2020-07-23 stsp
2204 e40622f4 2020-07-23 stsp do {
2205 e40622f4 2020-07-23 stsp err = resolve_symlink(&next_target,
2206 e40622f4 2020-07-23 stsp *link_target ? *link_target : path, commit_id, repo);
2207 e40622f4 2020-07-23 stsp if (err)
2208 e40622f4 2020-07-23 stsp break;
2209 e40622f4 2020-07-23 stsp if (next_target) {
2210 e40622f4 2020-07-23 stsp free(*link_target);
2211 e40622f4 2020-07-23 stsp if (--max_recursion == 0) {
2212 e40622f4 2020-07-23 stsp err = got_error_path(path, GOT_ERR_RECURSION);
2213 e40622f4 2020-07-23 stsp *link_target = NULL;
2214 e40622f4 2020-07-23 stsp break;
2215 e40622f4 2020-07-23 stsp }
2216 e40622f4 2020-07-23 stsp *link_target = next_target;
2217 e40622f4 2020-07-23 stsp }
2218 e40622f4 2020-07-23 stsp } while (next_target);
2219 e40622f4 2020-07-23 stsp
2220 e40622f4 2020-07-23 stsp return err;
2221 e40622f4 2020-07-23 stsp }
2222 e40622f4 2020-07-23 stsp
2223 e40622f4 2020-07-23 stsp const struct got_error *
2224 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
2225 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
2226 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
2227 ca6e02ac 2020-01-07 stsp {
2228 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
2229 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
2230 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
2231 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
2232 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
2233 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
2234 ca6e02ac 2020-01-07 stsp int idx;
2235 ca6e02ac 2020-01-07 stsp
2236 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
2237 ca6e02ac 2020-01-07 stsp if (err) {
2238 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
2239 ca6e02ac 2020-01-07 stsp return err;
2240 ca6e02ac 2020-01-07 stsp return NULL;
2241 ca6e02ac 2020-01-07 stsp }
2242 ca6e02ac 2020-01-07 stsp
2243 509c9973 2021-04-10 stsp err = got_packidx_get_packfile_path(&path_packfile, packidx);
2244 ca6e02ac 2020-01-07 stsp if (err)
2245 ca6e02ac 2020-01-07 stsp return err;
2246 ca6e02ac 2020-01-07 stsp
2247 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
2248 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
2249 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
2250 ca6e02ac 2020-01-07 stsp if (err)
2251 ca6e02ac 2020-01-07 stsp goto done;
2252 ca6e02ac 2020-01-07 stsp }
2253 ca6e02ac 2020-01-07 stsp
2254 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
2255 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
2256 ca6e02ac 2020-01-07 stsp if (err)
2257 ca6e02ac 2020-01-07 stsp goto done;
2258 ca6e02ac 2020-01-07 stsp }
2259 ca6e02ac 2020-01-07 stsp
2260 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
2261 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
2262 ca6e02ac 2020-01-07 stsp if (err)
2263 ca6e02ac 2020-01-07 stsp goto done;
2264 ca6e02ac 2020-01-07 stsp
2265 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
2266 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
2267 ca6e02ac 2020-01-07 stsp if (err)
2268 ca6e02ac 2020-01-07 stsp goto done;
2269 ca6e02ac 2020-01-07 stsp
2270 ca6e02ac 2020-01-07 stsp if (changed_commit) {
2271 ca6e02ac 2020-01-07 stsp /*
2272 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
2273 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
2274 ca6e02ac 2020-01-07 stsp */
2275 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
2276 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
2277 ca6e02ac 2020-01-07 stsp changed_commit);
2278 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
2279 ca6e02ac 2020-01-07 stsp }
2280 ca6e02ac 2020-01-07 stsp done:
2281 ca6e02ac 2020-01-07 stsp free(path_packfile);
2282 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
2283 ca6e02ac 2020-01-07 stsp return err;
2284 ed175427 2019-05-09 stsp }