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