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