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