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 876c234b 2018-09-10 stsp #include <sys/syslimits.h>
24 b48e2ddb 2019-05-22 stsp #include <sys/resource.h>
25 d1cda826 2017-11-06 stsp
26 a1fd68d8 2018-01-12 stsp #include <errno.h>
27 2178c42e 2018-04-22 stsp #include <fcntl.h>
28 d71d75ad 2017-11-05 stsp #include <stdio.h>
29 ab9a70b2 2017-11-06 stsp #include <stdlib.h>
30 ab9a70b2 2017-11-06 stsp #include <string.h>
31 2178c42e 2018-04-22 stsp #include <stdint.h>
32 d71d75ad 2017-11-05 stsp #include <sha1.h>
33 ab9a70b2 2017-11-06 stsp #include <zlib.h>
34 ab9a70b2 2017-11-06 stsp #include <ctype.h>
35 ab9a70b2 2017-11-06 stsp #include <limits.h>
36 2178c42e 2018-04-22 stsp #include <imsg.h>
37 788c352e 2018-06-16 stsp #include <time.h>
38 d71d75ad 2017-11-05 stsp
39 ab9a70b2 2017-11-06 stsp #include "got_error.h"
40 d71d75ad 2017-11-05 stsp #include "got_object.h"
41 ab9a70b2 2017-11-06 stsp #include "got_repository.h"
42 511a516b 2018-05-19 stsp #include "got_opentemp.h"
43 324d37e7 2019-05-11 stsp #include "got_path.h"
44 d71d75ad 2017-11-05 stsp
45 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
46 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
47 63581804 2018-07-09 stsp #include "got_lib_inflate.h"
48 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
49 2178c42e 2018-04-22 stsp #include "got_lib_privsep.h"
50 6bef87be 2018-09-11 stsp #include "got_lib_object_idcache.h"
51 6bef87be 2018-09-11 stsp #include "got_lib_object_cache.h"
52 ad242220 2018-09-08 stsp #include "got_lib_object_parse.h"
53 15a94983 2018-12-23 stsp #include "got_lib_pack.h"
54 93658fb9 2020-03-18 stsp #include "got_lib_fetch.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 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
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 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
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 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
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 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
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 56e0773d 2019-11-28 stsp }
865 56e0773d 2019-11-28 stsp
866 56e0773d 2019-11-28 stsp int
867 56e0773d 2019-11-28 stsp got_tree_entry_get_index(struct got_tree_entry *te)
868 56e0773d 2019-11-28 stsp {
869 56e0773d 2019-11-28 stsp return te->idx;
870 56e0773d 2019-11-28 stsp }
871 56e0773d 2019-11-28 stsp
872 56e0773d 2019-11-28 stsp struct got_tree_entry *
873 56e0773d 2019-11-28 stsp got_tree_entry_get_next(struct got_tree_object *tree,
874 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
875 56e0773d 2019-11-28 stsp {
876 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx + 1);
877 56e0773d 2019-11-28 stsp }
878 56e0773d 2019-11-28 stsp
879 56e0773d 2019-11-28 stsp struct got_tree_entry *
880 56e0773d 2019-11-28 stsp got_tree_entry_get_prev(struct got_tree_object *tree,
881 56e0773d 2019-11-28 stsp struct got_tree_entry *te)
882 56e0773d 2019-11-28 stsp {
883 56e0773d 2019-11-28 stsp return got_object_tree_get_entry(tree, te->idx - 1);
884 56e0773d 2019-11-28 stsp }
885 56e0773d 2019-11-28 stsp
886 3840f4c9 2018-09-12 stsp static const struct got_error *
887 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
888 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
889 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
890 3840f4c9 2018-09-12 stsp {
891 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
892 3840f4c9 2018-09-12 stsp int outfd_child;
893 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
894 24140570 2018-09-09 stsp
895 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
896 3840f4c9 2018-09-12 stsp if (basefd == -1)
897 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
898 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
899 3840f4c9 2018-09-12 stsp if (accumfd == -1)
900 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
901 3840f4c9 2018-09-12 stsp
902 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
903 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
904 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
905 3840f4c9 2018-09-12 stsp
906 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
907 3840f4c9 2018-09-12 stsp if (err)
908 3840f4c9 2018-09-12 stsp return err;
909 3840f4c9 2018-09-12 stsp
910 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
911 3840f4c9 2018-09-12 stsp outfd_child);
912 3840f4c9 2018-09-12 stsp if (err) {
913 41496140 2019-02-21 stsp close(basefd);
914 41496140 2019-02-21 stsp close(accumfd);
915 3840f4c9 2018-09-12 stsp return err;
916 3840f4c9 2018-09-12 stsp }
917 41496140 2019-02-21 stsp
918 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
919 3840f4c9 2018-09-12 stsp basefd);
920 3840f4c9 2018-09-12 stsp if (err) {
921 3840f4c9 2018-09-12 stsp close(accumfd);
922 3840f4c9 2018-09-12 stsp return err;
923 3840f4c9 2018-09-12 stsp }
924 3840f4c9 2018-09-12 stsp
925 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
926 3840f4c9 2018-09-12 stsp accumfd);
927 41496140 2019-02-21 stsp if (err)
928 3840f4c9 2018-09-12 stsp return err;
929 3840f4c9 2018-09-12 stsp
930 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
931 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
932 3840f4c9 2018-09-12 stsp if (err)
933 3840f4c9 2018-09-12 stsp return err;
934 3840f4c9 2018-09-12 stsp
935 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
936 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
937 3840f4c9 2018-09-12 stsp
938 3840f4c9 2018-09-12 stsp return err;
939 3840f4c9 2018-09-12 stsp }
940 3840f4c9 2018-09-12 stsp
941 ebc55e2d 2018-12-24 stsp static const struct got_error *
942 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
943 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
944 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
945 68482ea3 2017-11-27 stsp {
946 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
947 ebc55e2d 2018-12-24 stsp
948 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
949 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
950 ebc55e2d 2018-12-24 stsp if (err)
951 ebc55e2d 2018-12-24 stsp return err;
952 ebc55e2d 2018-12-24 stsp }
953 68482ea3 2017-11-27 stsp
954 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
955 ac544f8c 2019-01-13 stsp idx, id);
956 9f2369b0 2018-12-24 stsp }
957 9f2369b0 2018-12-24 stsp
958 9f2369b0 2018-12-24 stsp static const struct got_error *
959 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
960 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
961 9f2369b0 2018-12-24 stsp {
962 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
963 9f2369b0 2018-12-24 stsp int outfd_child;
964 9f2369b0 2018-12-24 stsp
965 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
966 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
967 638f9024 2019-05-13 stsp return got_error_from_errno("dup");
968 9f2369b0 2018-12-24 stsp
969 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
970 9f2369b0 2018-12-24 stsp if (err)
971 9f2369b0 2018-12-24 stsp return err;
972 9f2369b0 2018-12-24 stsp
973 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
974 41496140 2019-02-21 stsp if (err)
975 9f2369b0 2018-12-24 stsp return err;
976 9f2369b0 2018-12-24 stsp
977 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen, ibuf);
978 9f2369b0 2018-12-24 stsp if (err)
979 9f2369b0 2018-12-24 stsp return err;
980 9f2369b0 2018-12-24 stsp
981 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
982 638f9024 2019-05-13 stsp return got_error_from_errno("lseek");
983 9f2369b0 2018-12-24 stsp
984 9f2369b0 2018-12-24 stsp return err;
985 9f2369b0 2018-12-24 stsp }
986 9f2369b0 2018-12-24 stsp
987 9f2369b0 2018-12-24 stsp static const struct got_error *
988 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
989 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
990 9f2369b0 2018-12-24 stsp {
991 ddc7b220 2019-09-08 stsp const struct got_error *err;
992 9f2369b0 2018-12-24 stsp int imsg_fds[2];
993 9f2369b0 2018-12-24 stsp pid_t pid;
994 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
995 9f2369b0 2018-12-24 stsp
996 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
997 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
998 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
999 9f2369b0 2018-12-24 stsp }
1000 9f2369b0 2018-12-24 stsp
1001 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1002 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1003 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1004 9f2369b0 2018-12-24 stsp
1005 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1006 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1007 ddc7b220 2019-09-08 stsp free(ibuf);
1008 ddc7b220 2019-09-08 stsp return err;
1009 ddc7b220 2019-09-08 stsp }
1010 9f2369b0 2018-12-24 stsp
1011 9f2369b0 2018-12-24 stsp pid = fork();
1012 ddc7b220 2019-09-08 stsp if (pid == -1) {
1013 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1014 ddc7b220 2019-09-08 stsp free(ibuf);
1015 ddc7b220 2019-09-08 stsp return err;
1016 ddc7b220 2019-09-08 stsp }
1017 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1018 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
1019 9f2369b0 2018-12-24 stsp repo->path);
1020 9f2369b0 2018-12-24 stsp /* not reached */
1021 9f2369b0 2018-12-24 stsp }
1022 9f2369b0 2018-12-24 stsp
1023 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1024 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1025 ddc7b220 2019-09-08 stsp free(ibuf);
1026 ddc7b220 2019-09-08 stsp return err;
1027 ddc7b220 2019-09-08 stsp }
1028 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
1029 9f2369b0 2018-12-24 stsp imsg_fds[0];
1030 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
1031 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1032 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
1033 9f2369b0 2018-12-24 stsp
1034 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
1035 ebc55e2d 2018-12-24 stsp }
1036 68482ea3 2017-11-27 stsp
1037 ebc55e2d 2018-12-24 stsp static const struct got_error *
1038 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
1039 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
1040 ebc55e2d 2018-12-24 stsp {
1041 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
1042 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
1043 ebc55e2d 2018-12-24 stsp int idx;
1044 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1045 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
1046 e82b1d81 2019-07-27 stsp int outfd;
1047 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
1048 ebc55e2d 2018-12-24 stsp struct stat sb;
1049 ebc55e2d 2018-12-24 stsp
1050 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
1051 4558fcd4 2018-01-14 stsp if (*blob == NULL)
1052 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1053 68482ea3 2017-11-27 stsp
1054 55da3778 2018-09-10 stsp outfd = got_opentempfd();
1055 55da3778 2018-09-10 stsp if (outfd == -1)
1056 638f9024 2019-05-13 stsp return got_error_from_errno("got_opentempfd");
1057 55da3778 2018-09-10 stsp
1058 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
1059 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
1060 638f9024 2019-05-13 stsp err = got_error_from_errno("malloc");
1061 c7254d79 2018-04-24 stsp goto done;
1062 15c8b0e6 2018-04-24 stsp }
1063 ebc55e2d 2018-12-24 stsp
1064 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1065 e82b1d81 2019-07-27 stsp if (err == NULL) {
1066 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
1067 34f480ff 2019-07-27 stsp
1068 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1069 ebc55e2d 2018-12-24 stsp if (err)
1070 ebc55e2d 2018-12-24 stsp goto done;
1071 ebc55e2d 2018-12-24 stsp
1072 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1073 55da3778 2018-09-10 stsp if (pack == NULL) {
1074 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1075 ebc55e2d 2018-12-24 stsp packidx);
1076 55da3778 2018-09-10 stsp if (err)
1077 55da3778 2018-09-10 stsp goto done;
1078 55da3778 2018-09-10 stsp }
1079 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1080 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1081 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1082 e82b1d81 2019-07-27 stsp int infd;
1083 e82b1d81 2019-07-27 stsp
1084 e82b1d81 2019-07-27 stsp err = open_loose_object(&infd, id, repo);
1085 e82b1d81 2019-07-27 stsp if (err)
1086 e82b1d81 2019-07-27 stsp goto done;
1087 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1088 ac544f8c 2019-01-13 stsp repo);
1089 e82b1d81 2019-07-27 stsp }
1090 ebc55e2d 2018-12-24 stsp if (err)
1091 55da3778 2018-09-10 stsp goto done;
1092 2967a784 2018-04-24 stsp
1093 de060dff 2018-12-24 stsp if (hdrlen > size) {
1094 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1095 ebc55e2d 2018-12-24 stsp goto done;
1096 ebc55e2d 2018-12-24 stsp }
1097 ebc55e2d 2018-12-24 stsp
1098 ac544f8c 2019-01-13 stsp if (outbuf) {
1099 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1100 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1101 ac544f8c 2019-01-13 stsp outfd = -1;
1102 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1103 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1104 638f9024 2019-05-13 stsp err = got_error_from_errno("fmemopen");
1105 ac544f8c 2019-01-13 stsp free(outbuf);
1106 ac544f8c 2019-01-13 stsp goto done;
1107 ac544f8c 2019-01-13 stsp }
1108 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1109 ac544f8c 2019-01-13 stsp } else {
1110 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1111 638f9024 2019-05-13 stsp err = got_error_from_errno("fstat");
1112 ac544f8c 2019-01-13 stsp goto done;
1113 ac544f8c 2019-01-13 stsp }
1114 ac544f8c 2019-01-13 stsp
1115 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1116 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1117 ac544f8c 2019-01-13 stsp goto done;
1118 ac544f8c 2019-01-13 stsp }
1119 ac544f8c 2019-01-13 stsp
1120 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1121 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1122 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
1123 ac544f8c 2019-01-13 stsp close(outfd);
1124 ac544f8c 2019-01-13 stsp outfd = -1;
1125 ac544f8c 2019-01-13 stsp goto done;
1126 ac544f8c 2019-01-13 stsp }
1127 68482ea3 2017-11-27 stsp }
1128 68482ea3 2017-11-27 stsp
1129 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1130 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1131 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1132 7d283eee 2017-11-29 stsp
1133 c7254d79 2018-04-24 stsp done:
1134 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1135 55da3778 2018-09-10 stsp if (err) {
1136 55da3778 2018-09-10 stsp if (*blob) {
1137 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1138 55da3778 2018-09-10 stsp *blob = NULL;
1139 55da3778 2018-09-10 stsp } else if (outfd != -1)
1140 55da3778 2018-09-10 stsp close(outfd);
1141 a19581a2 2018-06-21 stsp }
1142 a19581a2 2018-06-21 stsp return err;
1143 a19581a2 2018-06-21 stsp }
1144 a19581a2 2018-06-21 stsp
1145 a19581a2 2018-06-21 stsp const struct got_error *
1146 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1147 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1148 a19581a2 2018-06-21 stsp size_t blocksize)
1149 a19581a2 2018-06-21 stsp {
1150 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1151 ebc55e2d 2018-12-24 stsp }
1152 835e0dbd 2018-06-21 stsp
1153 ebc55e2d 2018-12-24 stsp const struct got_error *
1154 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1155 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1156 ebc55e2d 2018-12-24 stsp {
1157 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1158 0ffeb3c2 2017-11-26 stsp }
1159 68482ea3 2017-11-27 stsp
1160 fb43ecf1 2019-02-11 stsp const struct got_error *
1161 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1162 68482ea3 2017-11-27 stsp {
1163 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1164 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1165 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1166 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
1167 ac544f8c 2019-01-13 stsp free(blob->data);
1168 68482ea3 2017-11-27 stsp free(blob);
1169 fb43ecf1 2019-02-11 stsp return err;
1170 f934cf2c 2018-02-12 stsp }
1171 f934cf2c 2018-02-12 stsp
1172 f934cf2c 2018-02-12 stsp char *
1173 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1174 f934cf2c 2018-02-12 stsp {
1175 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1176 f934cf2c 2018-02-12 stsp }
1177 f934cf2c 2018-02-12 stsp
1178 f934cf2c 2018-02-12 stsp size_t
1179 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1180 f934cf2c 2018-02-12 stsp {
1181 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1182 68482ea3 2017-11-27 stsp }
1183 68482ea3 2017-11-27 stsp
1184 f934cf2c 2018-02-12 stsp const uint8_t *
1185 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1186 f934cf2c 2018-02-12 stsp {
1187 f934cf2c 2018-02-12 stsp return blob->read_buf;
1188 f934cf2c 2018-02-12 stsp }
1189 f934cf2c 2018-02-12 stsp
1190 68482ea3 2017-11-27 stsp const struct got_error *
1191 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1192 68482ea3 2017-11-27 stsp {
1193 eb651edf 2018-02-11 stsp size_t n;
1194 eb651edf 2018-02-11 stsp
1195 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1196 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1197 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1198 eb651edf 2018-02-11 stsp *outlenp = n;
1199 35e9ba5d 2018-06-21 stsp return NULL;
1200 35e9ba5d 2018-06-21 stsp }
1201 35e9ba5d 2018-06-21 stsp
1202 35e9ba5d 2018-06-21 stsp const struct got_error *
1203 f595d9bd 2019-08-14 stsp got_object_blob_dump_to_file(size_t *filesize, int *nlines,
1204 6c4c42e0 2019-06-24 stsp off_t **line_offsets, FILE *outfile, struct got_blob_object *blob)
1205 35e9ba5d 2018-06-21 stsp {
1206 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1207 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1208 84451b3e 2018-07-10 stsp const uint8_t *buf;
1209 84451b3e 2018-07-10 stsp int i;
1210 6c4c42e0 2019-06-24 stsp size_t noffsets = 0;
1211 f595d9bd 2019-08-14 stsp off_t off = 0, total_len = 0;
1212 84451b3e 2018-07-10 stsp
1213 6c4c42e0 2019-06-24 stsp if (line_offsets)
1214 6c4c42e0 2019-06-24 stsp *line_offsets = NULL;
1215 f595d9bd 2019-08-14 stsp if (filesize)
1216 f595d9bd 2019-08-14 stsp *filesize = 0;
1217 84451b3e 2018-07-10 stsp if (nlines)
1218 84451b3e 2018-07-10 stsp *nlines = 0;
1219 35e9ba5d 2018-06-21 stsp
1220 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1221 35e9ba5d 2018-06-21 stsp do {
1222 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1223 35e9ba5d 2018-06-21 stsp if (err)
1224 35e9ba5d 2018-06-21 stsp return err;
1225 35e9ba5d 2018-06-21 stsp if (len == 0)
1226 35e9ba5d 2018-06-21 stsp break;
1227 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1228 b02560ec 2019-08-19 stsp i = hdrlen;
1229 78695fb7 2019-08-12 stsp if (line_offsets && nlines) {
1230 78695fb7 2019-08-12 stsp if (*line_offsets == NULL) {
1231 78695fb7 2019-08-12 stsp /* Have some data but perhaps no '\n'. */
1232 78695fb7 2019-08-12 stsp noffsets = 1;
1233 78695fb7 2019-08-12 stsp *nlines = 1;
1234 f595d9bd 2019-08-14 stsp *line_offsets = calloc(1, sizeof(**line_offsets));
1235 78695fb7 2019-08-12 stsp if (*line_offsets == NULL)
1236 845785d4 2020-02-02 tracey return got_error_from_errno("calloc");
1237 b02560ec 2019-08-19 stsp
1238 b02560ec 2019-08-19 stsp /* Skip forward over end of first line. */
1239 b02560ec 2019-08-19 stsp while (i < len) {
1240 b02560ec 2019-08-19 stsp if (buf[i] == '\n')
1241 b02560ec 2019-08-19 stsp break;
1242 b02560ec 2019-08-19 stsp i++;
1243 b02560ec 2019-08-19 stsp }
1244 b02560ec 2019-08-19 stsp }
1245 b02560ec 2019-08-19 stsp /* Scan '\n' offsets in remaining chunk of data. */
1246 b02560ec 2019-08-19 stsp while (i < len) {
1247 b02560ec 2019-08-19 stsp if (buf[i] != '\n') {
1248 b02560ec 2019-08-19 stsp i++;
1249 f595d9bd 2019-08-14 stsp continue;
1250 b02560ec 2019-08-19 stsp }
1251 f595d9bd 2019-08-14 stsp (*nlines)++;
1252 78695fb7 2019-08-12 stsp if (noffsets < *nlines) {
1253 78695fb7 2019-08-12 stsp off_t *o = recallocarray(*line_offsets,
1254 78695fb7 2019-08-12 stsp noffsets, *nlines,
1255 78695fb7 2019-08-12 stsp sizeof(**line_offsets));
1256 78695fb7 2019-08-12 stsp if (o == NULL) {
1257 78695fb7 2019-08-12 stsp free(*line_offsets);
1258 78695fb7 2019-08-12 stsp *line_offsets = NULL;
1259 78695fb7 2019-08-12 stsp return got_error_from_errno(
1260 78695fb7 2019-08-12 stsp "recallocarray");
1261 78695fb7 2019-08-12 stsp }
1262 78695fb7 2019-08-12 stsp *line_offsets = o;
1263 78695fb7 2019-08-12 stsp noffsets = *nlines;
1264 78695fb7 2019-08-12 stsp }
1265 f595d9bd 2019-08-14 stsp off = total_len + i - hdrlen + 1;
1266 f595d9bd 2019-08-14 stsp (*line_offsets)[*nlines - 1] = off;
1267 b02560ec 2019-08-19 stsp i++;
1268 6c4c42e0 2019-06-24 stsp }
1269 84451b3e 2018-07-10 stsp }
1270 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1271 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1272 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1273 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1274 f595d9bd 2019-08-14 stsp total_len += len - hdrlen;
1275 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1276 35e9ba5d 2018-06-21 stsp } while (len != 0);
1277 35e9ba5d 2018-06-21 stsp
1278 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1279 638f9024 2019-05-13 stsp return got_error_from_errno("fflush");
1280 35e9ba5d 2018-06-21 stsp rewind(outfile);
1281 35e9ba5d 2018-06-21 stsp
1282 f595d9bd 2019-08-14 stsp if (filesize)
1283 f595d9bd 2019-08-14 stsp *filesize = total_len;
1284 f595d9bd 2019-08-14 stsp
1285 776d4d29 2018-06-17 stsp return NULL;
1286 f4a881ce 2018-11-17 stsp }
1287 f4a881ce 2018-11-17 stsp
1288 f4a881ce 2018-11-17 stsp static const struct got_error *
1289 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1290 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1291 a158c901 2018-12-23 stsp {
1292 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1293 a158c901 2018-12-23 stsp
1294 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1295 268f7291 2018-12-24 stsp pack_idx);
1296 a158c901 2018-12-23 stsp if (err)
1297 a158c901 2018-12-23 stsp return err;
1298 a158c901 2018-12-23 stsp
1299 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1300 268f7291 2018-12-24 stsp }
1301 268f7291 2018-12-24 stsp
1302 268f7291 2018-12-24 stsp static const struct got_error *
1303 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1304 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1305 268f7291 2018-12-24 stsp struct got_object_id *id)
1306 268f7291 2018-12-24 stsp {
1307 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1308 268f7291 2018-12-24 stsp
1309 268f7291 2018-12-24 stsp if (pack->privsep_child)
1310 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1311 268f7291 2018-12-24 stsp
1312 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1313 268f7291 2018-12-24 stsp if (err)
1314 268f7291 2018-12-24 stsp return err;
1315 268f7291 2018-12-24 stsp
1316 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1317 a158c901 2018-12-23 stsp }
1318 9f2369b0 2018-12-24 stsp
1319 9f2369b0 2018-12-24 stsp static const struct got_error *
1320 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1321 9f2369b0 2018-12-24 stsp int fd)
1322 9f2369b0 2018-12-24 stsp {
1323 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1324 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1325 9f2369b0 2018-12-24 stsp
1326 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1327 9f2369b0 2018-12-24 stsp
1328 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1329 9f2369b0 2018-12-24 stsp if (err)
1330 9f2369b0 2018-12-24 stsp return err;
1331 9f2369b0 2018-12-24 stsp
1332 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1333 9f2369b0 2018-12-24 stsp }
1334 9f2369b0 2018-12-24 stsp
1335 9f2369b0 2018-12-24 stsp static const struct got_error *
1336 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1337 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1338 9f2369b0 2018-12-24 stsp {
1339 ddc7b220 2019-09-08 stsp const struct got_error *err;
1340 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1341 9f2369b0 2018-12-24 stsp pid_t pid;
1342 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1343 9f2369b0 2018-12-24 stsp
1344 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1345 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1346 9f2369b0 2018-12-24 stsp
1347 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1348 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1349 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1350 9f2369b0 2018-12-24 stsp
1351 ddc7b220 2019-09-08 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
1352 ddc7b220 2019-09-08 stsp err = got_error_from_errno("socketpair");
1353 ddc7b220 2019-09-08 stsp free(ibuf);
1354 ddc7b220 2019-09-08 stsp return err;
1355 ddc7b220 2019-09-08 stsp }
1356 9f2369b0 2018-12-24 stsp
1357 9f2369b0 2018-12-24 stsp pid = fork();
1358 ddc7b220 2019-09-08 stsp if (pid == -1) {
1359 ddc7b220 2019-09-08 stsp err = got_error_from_errno("fork");
1360 ddc7b220 2019-09-08 stsp free(ibuf);
1361 ddc7b220 2019-09-08 stsp return err;
1362 ddc7b220 2019-09-08 stsp }
1363 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1364 aba9c984 2019-09-08 stsp got_privsep_exec_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1365 9f2369b0 2018-12-24 stsp repo->path);
1366 9f2369b0 2018-12-24 stsp /* not reached */
1367 9f2369b0 2018-12-24 stsp }
1368 9f2369b0 2018-12-24 stsp
1369 ddc7b220 2019-09-08 stsp if (close(imsg_fds[1]) != 0) {
1370 ddc7b220 2019-09-08 stsp err = got_error_from_errno("close");
1371 ddc7b220 2019-09-08 stsp free(ibuf);
1372 ddc7b220 2019-09-08 stsp return err;
1373 ddc7b220 2019-09-08 stsp }
1374 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1375 9f2369b0 2018-12-24 stsp imsg_fds[0];
1376 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1377 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1378 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1379 a158c901 2018-12-23 stsp
1380 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1381 9f2369b0 2018-12-24 stsp }
1382 a158c901 2018-12-23 stsp
1383 a158c901 2018-12-23 stsp static const struct got_error *
1384 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1385 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1386 f4a881ce 2018-11-17 stsp {
1387 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1388 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1389 e82b1d81 2019-07-27 stsp int idx;
1390 8d2c5ea3 2019-08-13 stsp char *path_packfile = NULL;
1391 5d844a1e 2019-08-13 stsp struct got_object *obj = NULL;
1392 5d844a1e 2019-08-13 stsp int obj_type = GOT_OBJ_TYPE_ANY;
1393 f4a881ce 2018-11-17 stsp
1394 f4a881ce 2018-11-17 stsp if (check_cache) {
1395 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1396 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1397 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1398 f4a881ce 2018-11-17 stsp return NULL;
1399 f4a881ce 2018-11-17 stsp }
1400 f4a881ce 2018-11-17 stsp } else
1401 f4a881ce 2018-11-17 stsp *tag = NULL;
1402 f4a881ce 2018-11-17 stsp
1403 e82b1d81 2019-07-27 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1404 e82b1d81 2019-07-27 stsp if (err == NULL) {
1405 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1406 f4a881ce 2018-11-17 stsp
1407 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1408 268f7291 2018-12-24 stsp if (err)
1409 268f7291 2018-12-24 stsp return err;
1410 268f7291 2018-12-24 stsp
1411 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1412 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1413 e82b1d81 2019-07-27 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1414 e82b1d81 2019-07-27 stsp packidx);
1415 f4a881ce 2018-11-17 stsp if (err)
1416 8d2c5ea3 2019-08-13 stsp goto done;
1417 f4a881ce 2018-11-17 stsp }
1418 5d844a1e 2019-08-13 stsp
1419 992eb9d8 2020-02-07 tracey /* Beware of "lightweight" tags: Check object type first. */
1420 5d844a1e 2019-08-13 stsp err = read_packed_object_privsep(&obj, repo, pack, packidx,
1421 5d844a1e 2019-08-13 stsp idx, id);
1422 5d844a1e 2019-08-13 stsp if (err)
1423 5d844a1e 2019-08-13 stsp goto done;
1424 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1425 5d844a1e 2019-08-13 stsp got_object_close(obj);
1426 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
1427 5d844a1e 2019-08-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1428 5d844a1e 2019-08-13 stsp goto done;
1429 5d844a1e 2019-08-13 stsp }
1430 5d844a1e 2019-08-13 stsp err = read_packed_tag_privsep(tag, pack, packidx, idx, id);
1431 e82b1d81 2019-07-27 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1432 e82b1d81 2019-07-27 stsp int fd;
1433 e82b1d81 2019-07-27 stsp
1434 e82b1d81 2019-07-27 stsp err = open_loose_object(&fd, id, repo);
1435 e82b1d81 2019-07-27 stsp if (err)
1436 e82b1d81 2019-07-27 stsp return err;
1437 5d844a1e 2019-08-13 stsp err = read_object_header_privsep(&obj, repo, fd);
1438 5d844a1e 2019-08-13 stsp if (err)
1439 5d844a1e 2019-08-13 stsp return err;
1440 5d844a1e 2019-08-13 stsp obj_type = obj->type;
1441 5d844a1e 2019-08-13 stsp got_object_close(obj);
1442 5d844a1e 2019-08-13 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1443 5d844a1e 2019-08-13 stsp return got_error(GOT_ERR_OBJ_TYPE);
1444 5d844a1e 2019-08-13 stsp
1445 5d844a1e 2019-08-13 stsp err = open_loose_object(&fd, id, repo);
1446 5d844a1e 2019-08-13 stsp if (err)
1447 5d844a1e 2019-08-13 stsp return err;
1448 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1449 e82b1d81 2019-07-27 stsp }
1450 f4a881ce 2018-11-17 stsp
1451 f4a881ce 2018-11-17 stsp if (err == NULL) {
1452 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1453 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1454 f4a881ce 2018-11-17 stsp }
1455 8d2c5ea3 2019-08-13 stsp done:
1456 8d2c5ea3 2019-08-13 stsp free(path_packfile);
1457 f4a881ce 2018-11-17 stsp return err;
1458 f4a881ce 2018-11-17 stsp }
1459 f4a881ce 2018-11-17 stsp
1460 f4a881ce 2018-11-17 stsp const struct got_error *
1461 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1462 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1463 f4a881ce 2018-11-17 stsp {
1464 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1465 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1466 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1467 f4a881ce 2018-11-17 stsp return NULL;
1468 f4a881ce 2018-11-17 stsp }
1469 f4a881ce 2018-11-17 stsp
1470 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1471 f4a881ce 2018-11-17 stsp }
1472 f4a881ce 2018-11-17 stsp
1473 f4a881ce 2018-11-17 stsp const struct got_error *
1474 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1475 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1476 f4a881ce 2018-11-17 stsp {
1477 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1478 d24820bf 2019-08-11 stsp }
1479 d24820bf 2019-08-11 stsp
1480 d24820bf 2019-08-11 stsp const char *
1481 d24820bf 2019-08-11 stsp got_object_tag_get_name(struct got_tag_object *tag)
1482 d24820bf 2019-08-11 stsp {
1483 d24820bf 2019-08-11 stsp return tag->tag;
1484 0bd18d37 2019-02-01 stsp }
1485 0bd18d37 2019-02-01 stsp
1486 0bd18d37 2019-02-01 stsp int
1487 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1488 0bd18d37 2019-02-01 stsp {
1489 0bd18d37 2019-02-01 stsp return tag->obj_type;
1490 0bd18d37 2019-02-01 stsp }
1491 0bd18d37 2019-02-01 stsp
1492 0bd18d37 2019-02-01 stsp struct got_object_id *
1493 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1494 0bd18d37 2019-02-01 stsp {
1495 0bd18d37 2019-02-01 stsp return &tag->id;
1496 01073a5d 2019-08-22 stsp }
1497 01073a5d 2019-08-22 stsp
1498 01073a5d 2019-08-22 stsp time_t
1499 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_time(struct got_tag_object *tag)
1500 01073a5d 2019-08-22 stsp {
1501 01073a5d 2019-08-22 stsp return tag->tagger_time;
1502 01073a5d 2019-08-22 stsp }
1503 01073a5d 2019-08-22 stsp
1504 01073a5d 2019-08-22 stsp time_t
1505 01073a5d 2019-08-22 stsp got_object_tag_get_tagger_gmtoff(struct got_tag_object *tag)
1506 01073a5d 2019-08-22 stsp {
1507 01073a5d 2019-08-22 stsp return tag->tagger_gmtoff;
1508 01073a5d 2019-08-22 stsp }
1509 01073a5d 2019-08-22 stsp
1510 01073a5d 2019-08-22 stsp const char *
1511 01073a5d 2019-08-22 stsp got_object_tag_get_tagger(struct got_tag_object *tag)
1512 01073a5d 2019-08-22 stsp {
1513 01073a5d 2019-08-22 stsp return tag->tagger;
1514 776d4d29 2018-06-17 stsp }
1515 776d4d29 2018-06-17 stsp
1516 01073a5d 2019-08-22 stsp const char *
1517 01073a5d 2019-08-22 stsp got_object_tag_get_message(struct got_tag_object *tag)
1518 01073a5d 2019-08-22 stsp {
1519 01073a5d 2019-08-22 stsp return tag->tagmsg;
1520 01073a5d 2019-08-22 stsp }
1521 01073a5d 2019-08-22 stsp
1522 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1523 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1524 776d4d29 2018-06-17 stsp {
1525 56e0773d 2019-11-28 stsp int i;
1526 776d4d29 2018-06-17 stsp
1527 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1528 56e0773d 2019-11-28 stsp for (i = 0; i < tree->nentries; i++) {
1529 56e0773d 2019-11-28 stsp struct got_tree_entry *te = &tree->entries[i];
1530 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1531 63da309a 2018-11-07 stsp if (cmp < 0)
1532 63da309a 2018-11-07 stsp continue;
1533 63da309a 2018-11-07 stsp if (cmp > 0)
1534 63da309a 2018-11-07 stsp break;
1535 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1536 776d4d29 2018-06-17 stsp return te;
1537 776d4d29 2018-06-17 stsp }
1538 eb651edf 2018-02-11 stsp return NULL;
1539 a129376b 2019-03-28 stsp }
1540 a129376b 2019-03-28 stsp
1541 56e0773d 2019-11-28 stsp struct got_tree_entry *
1542 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1543 a129376b 2019-03-28 stsp {
1544 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1545 776d4d29 2018-06-17 stsp }
1546 776d4d29 2018-06-17 stsp
1547 776d4d29 2018-06-17 stsp const struct got_error *
1548 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1549 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1550 776d4d29 2018-06-17 stsp {
1551 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1552 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1553 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1554 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1555 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1556 b7cd37e5 2018-11-18 stsp size_t seglen;
1557 776d4d29 2018-06-17 stsp
1558 27d434c2 2018-09-15 stsp *id = NULL;
1559 776d4d29 2018-06-17 stsp
1560 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1561 776d4d29 2018-06-17 stsp if (err)
1562 776d4d29 2018-06-17 stsp goto done;
1563 776d4d29 2018-06-17 stsp
1564 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1565 5e54fb30 2019-05-31 stsp if (got_path_is_root_dir(path)) {
1566 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1567 27d434c2 2018-09-15 stsp if (*id == NULL)
1568 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_id_dup");
1569 ca008b32 2018-07-22 stsp goto done;
1570 776d4d29 2018-06-17 stsp }
1571 776d4d29 2018-06-17 stsp
1572 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1573 776d4d29 2018-06-17 stsp if (err)
1574 776d4d29 2018-06-17 stsp goto done;
1575 776d4d29 2018-06-17 stsp
1576 65a9bbe9 2018-09-15 stsp s = path;
1577 5e54fb30 2019-05-31 stsp while (s[0] == '/')
1578 5e54fb30 2019-05-31 stsp s++;
1579 776d4d29 2018-06-17 stsp seg = s;
1580 65a9bbe9 2018-09-15 stsp seglen = 0;
1581 b7cd37e5 2018-11-18 stsp while (*s) {
1582 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1583 776d4d29 2018-06-17 stsp
1584 776d4d29 2018-06-17 stsp if (*s != '/') {
1585 776d4d29 2018-06-17 stsp s++;
1586 65a9bbe9 2018-09-15 stsp seglen++;
1587 00530cfb 2018-06-21 stsp if (*s)
1588 00530cfb 2018-06-21 stsp continue;
1589 776d4d29 2018-06-17 stsp }
1590 776d4d29 2018-06-17 stsp
1591 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1592 db37e2c0 2018-06-21 stsp if (te == NULL) {
1593 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1594 776d4d29 2018-06-17 stsp goto done;
1595 776d4d29 2018-06-17 stsp }
1596 776d4d29 2018-06-17 stsp
1597 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1598 67606321 2018-06-21 stsp break;
1599 67606321 2018-06-21 stsp
1600 776d4d29 2018-06-17 stsp seg = s + 1;
1601 65a9bbe9 2018-09-15 stsp seglen = 0;
1602 776d4d29 2018-06-17 stsp s++;
1603 776d4d29 2018-06-17 stsp if (*s) {
1604 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1605 56e0773d 2019-11-28 stsp &te->id);
1606 db37e2c0 2018-06-21 stsp te = NULL;
1607 776d4d29 2018-06-17 stsp if (err)
1608 776d4d29 2018-06-17 stsp goto done;
1609 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1610 776d4d29 2018-06-17 stsp tree = next_tree;
1611 776d4d29 2018-06-17 stsp }
1612 776d4d29 2018-06-17 stsp }
1613 776d4d29 2018-06-17 stsp
1614 27d434c2 2018-09-15 stsp if (te) {
1615 56e0773d 2019-11-28 stsp *id = got_object_id_dup(&te->id);
1616 27d434c2 2018-09-15 stsp if (*id == NULL)
1617 638f9024 2019-05-13 stsp return got_error_from_errno("got_object_id_dup");
1618 27d434c2 2018-09-15 stsp } else
1619 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1620 776d4d29 2018-06-17 stsp done:
1621 776d4d29 2018-06-17 stsp if (commit)
1622 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1623 776d4d29 2018-06-17 stsp if (tree)
1624 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1625 776d4d29 2018-06-17 stsp return err;
1626 68482ea3 2017-11-27 stsp }
1627 07862c20 2018-09-15 stsp
1628 07862c20 2018-09-15 stsp const struct got_error *
1629 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1630 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1631 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1632 07862c20 2018-09-15 stsp {
1633 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1634 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1635 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1636 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1637 3b7f9878 2018-11-18 stsp size_t seglen;
1638 07862c20 2018-09-15 stsp
1639 07862c20 2018-09-15 stsp *changed = 0;
1640 07862c20 2018-09-15 stsp
1641 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1642 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
1643 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
1644 07862c20 2018-09-15 stsp
1645 07862c20 2018-09-15 stsp tree1 = tree01;
1646 07862c20 2018-09-15 stsp tree2 = tree02;
1647 65a9bbe9 2018-09-15 stsp s = path;
1648 61a7d79f 2020-02-29 stsp while (*s == '/')
1649 61a7d79f 2020-02-29 stsp s++;
1650 07862c20 2018-09-15 stsp seg = s;
1651 65a9bbe9 2018-09-15 stsp seglen = 0;
1652 3b7f9878 2018-11-18 stsp while (*s) {
1653 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1654 07862c20 2018-09-15 stsp
1655 07862c20 2018-09-15 stsp if (*s != '/') {
1656 07862c20 2018-09-15 stsp s++;
1657 65a9bbe9 2018-09-15 stsp seglen++;
1658 07862c20 2018-09-15 stsp if (*s)
1659 07862c20 2018-09-15 stsp continue;
1660 07862c20 2018-09-15 stsp }
1661 07862c20 2018-09-15 stsp
1662 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1663 07862c20 2018-09-15 stsp if (te1 == NULL) {
1664 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1665 07862c20 2018-09-15 stsp goto done;
1666 07862c20 2018-09-15 stsp }
1667 07862c20 2018-09-15 stsp
1668 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1669 07862c20 2018-09-15 stsp if (te2 == NULL) {
1670 07862c20 2018-09-15 stsp *changed = 1;
1671 07862c20 2018-09-15 stsp goto done;
1672 07862c20 2018-09-15 stsp }
1673 07862c20 2018-09-15 stsp
1674 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1675 07862c20 2018-09-15 stsp *changed = 1;
1676 07862c20 2018-09-15 stsp goto done;
1677 07862c20 2018-09-15 stsp }
1678 07862c20 2018-09-15 stsp
1679 56e0773d 2019-11-28 stsp if (got_object_id_cmp(&te1->id, &te2->id) == 0) {
1680 07862c20 2018-09-15 stsp *changed = 0;
1681 07862c20 2018-09-15 stsp goto done;
1682 07862c20 2018-09-15 stsp }
1683 07862c20 2018-09-15 stsp
1684 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1685 07862c20 2018-09-15 stsp *changed = 1;
1686 07862c20 2018-09-15 stsp goto done;
1687 07862c20 2018-09-15 stsp }
1688 07862c20 2018-09-15 stsp
1689 07862c20 2018-09-15 stsp seg = s + 1;
1690 07862c20 2018-09-15 stsp s++;
1691 65a9bbe9 2018-09-15 stsp seglen = 0;
1692 07862c20 2018-09-15 stsp if (*s) {
1693 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1694 56e0773d 2019-11-28 stsp &te1->id);
1695 07862c20 2018-09-15 stsp te1 = NULL;
1696 07862c20 2018-09-15 stsp if (err)
1697 07862c20 2018-09-15 stsp goto done;
1698 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1699 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1700 07862c20 2018-09-15 stsp tree1 = next_tree1;
1701 07862c20 2018-09-15 stsp
1702 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1703 56e0773d 2019-11-28 stsp &te2->id);
1704 07862c20 2018-09-15 stsp te2 = NULL;
1705 07862c20 2018-09-15 stsp if (err)
1706 07862c20 2018-09-15 stsp goto done;
1707 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1708 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1709 07862c20 2018-09-15 stsp tree2 = next_tree2;
1710 07862c20 2018-09-15 stsp }
1711 07862c20 2018-09-15 stsp }
1712 07862c20 2018-09-15 stsp done:
1713 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1714 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1715 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1716 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1717 77880158 2018-11-04 stsp return err;
1718 77880158 2018-11-04 stsp }
1719 ed175427 2019-05-09 stsp
1720 ed175427 2019-05-09 stsp const struct got_error *
1721 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1722 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1723 ed175427 2019-05-09 stsp {
1724 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1725 ed175427 2019-05-09 stsp
1726 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1727 ed175427 2019-05-09 stsp if (*new_te == NULL)
1728 638f9024 2019-05-13 stsp return got_error_from_errno("calloc");
1729 ed175427 2019-05-09 stsp
1730 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1731 56e0773d 2019-11-28 stsp memcpy((*new_te)->name, te->name, sizeof((*new_te)->name));
1732 56e0773d 2019-11-28 stsp memcpy(&(*new_te)->id, &te->id, sizeof((*new_te)->id));
1733 8c4eabf2 2019-05-10 stsp return err;
1734 63c5ca5d 2019-08-24 stsp }
1735 63c5ca5d 2019-08-24 stsp
1736 63c5ca5d 2019-08-24 stsp int
1737 56e0773d 2019-11-28 stsp got_object_tree_entry_is_submodule(struct got_tree_entry *te)
1738 63c5ca5d 2019-08-24 stsp {
1739 63c5ca5d 2019-08-24 stsp return (te->mode & S_IFMT) == (S_IFDIR | S_IFLNK);
1740 ca6e02ac 2020-01-07 stsp }
1741 ca6e02ac 2020-01-07 stsp
1742 ca6e02ac 2020-01-07 stsp const struct got_error *
1743 ca6e02ac 2020-01-07 stsp got_traverse_packed_commits(struct got_object_id_queue *traversed_commits,
1744 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_id, const char *path,
1745 ca6e02ac 2020-01-07 stsp struct got_repository *repo)
1746 ca6e02ac 2020-01-07 stsp {
1747 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
1748 ca6e02ac 2020-01-07 stsp struct got_pack *pack = NULL;
1749 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx = NULL;
1750 ca6e02ac 2020-01-07 stsp char *path_packfile = NULL;
1751 ca6e02ac 2020-01-07 stsp struct got_commit_object *changed_commit = NULL;
1752 ca6e02ac 2020-01-07 stsp struct got_object_id *changed_commit_id = NULL;
1753 ca6e02ac 2020-01-07 stsp int idx;
1754 ca6e02ac 2020-01-07 stsp
1755 ca6e02ac 2020-01-07 stsp err = got_repo_search_packidx(&packidx, &idx, repo, commit_id);
1756 ca6e02ac 2020-01-07 stsp if (err) {
1757 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
1758 ca6e02ac 2020-01-07 stsp return err;
1759 ca6e02ac 2020-01-07 stsp return NULL;
1760 ca6e02ac 2020-01-07 stsp }
1761 ca6e02ac 2020-01-07 stsp
1762 ca6e02ac 2020-01-07 stsp err = get_packfile_path(&path_packfile, packidx);
1763 ca6e02ac 2020-01-07 stsp if (err)
1764 ca6e02ac 2020-01-07 stsp return err;
1765 ca6e02ac 2020-01-07 stsp
1766 ca6e02ac 2020-01-07 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1767 ca6e02ac 2020-01-07 stsp if (pack == NULL) {
1768 ca6e02ac 2020-01-07 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
1769 ca6e02ac 2020-01-07 stsp if (err)
1770 ca6e02ac 2020-01-07 stsp goto done;
1771 ca6e02ac 2020-01-07 stsp }
1772 ca6e02ac 2020-01-07 stsp
1773 ca6e02ac 2020-01-07 stsp if (pack->privsep_child == NULL) {
1774 ca6e02ac 2020-01-07 stsp err = start_pack_privsep_child(pack, packidx);
1775 ca6e02ac 2020-01-07 stsp if (err)
1776 ca6e02ac 2020-01-07 stsp goto done;
1777 ca6e02ac 2020-01-07 stsp }
1778 ca6e02ac 2020-01-07 stsp
1779 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit_traversal_request(
1780 ca6e02ac 2020-01-07 stsp pack->privsep_child->ibuf, commit_id, idx, path);
1781 ca6e02ac 2020-01-07 stsp if (err)
1782 ca6e02ac 2020-01-07 stsp goto done;
1783 ca6e02ac 2020-01-07 stsp
1784 ca6e02ac 2020-01-07 stsp err = got_privsep_recv_traversed_commits(&changed_commit,
1785 ca6e02ac 2020-01-07 stsp &changed_commit_id, traversed_commits, pack->privsep_child->ibuf);
1786 ca6e02ac 2020-01-07 stsp if (err)
1787 ca6e02ac 2020-01-07 stsp goto done;
1788 ca6e02ac 2020-01-07 stsp
1789 ca6e02ac 2020-01-07 stsp if (changed_commit) {
1790 ca6e02ac 2020-01-07 stsp /*
1791 ca6e02ac 2020-01-07 stsp * Cache the commit in which the path was changed.
1792 ca6e02ac 2020-01-07 stsp * This commit might be opened again soon.
1793 ca6e02ac 2020-01-07 stsp */
1794 ca6e02ac 2020-01-07 stsp changed_commit->refcnt++;
1795 ca6e02ac 2020-01-07 stsp err = got_repo_cache_commit(repo, changed_commit_id,
1796 ca6e02ac 2020-01-07 stsp changed_commit);
1797 ca6e02ac 2020-01-07 stsp got_object_commit_close(changed_commit);
1798 ca6e02ac 2020-01-07 stsp }
1799 ca6e02ac 2020-01-07 stsp done:
1800 ca6e02ac 2020-01-07 stsp free(path_packfile);
1801 ca6e02ac 2020-01-07 stsp free(changed_commit_id);
1802 ca6e02ac 2020-01-07 stsp return err;
1803 ed175427 2019-05-09 stsp }