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