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