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