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