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