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