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