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