2 a1fd68d8 2018-01-12 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
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.
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.
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>
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>
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"
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"
56 ab9a70b2 2017-11-06 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
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)
62 8bf5b3c9 2018-03-17 stsp struct got_object_id *id2;
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));
71 3235492e 2018-04-01 stsp struct got_object_id *
72 3235492e 2018-04-01 stsp got_object_get_id(struct got_object *obj)
74 6402fb3c 2018-09-15 stsp return &obj->id;
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)
80 bacc9935 2018-05-20 stsp return got_object_id_str(outbuf, &obj->id);
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)
87 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
88 15a94983 2018-12-23 stsp struct got_object *obj;
90 15a94983 2018-12-23 stsp err = got_object_open(&obj, repo, id);
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;
102 15a94983 2018-12-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
106 15a94983 2018-12-23 stsp got_object_close(obj);
107 15a94983 2018-12-23 stsp return err;
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)
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);
117 e6b1056e 2018-04-22 stsp *path = NULL;
119 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
120 0a585a0d 2018-03-17 stsp return got_error_from_errno();
122 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
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();
132 d1cda826 2017-11-06 stsp free(path_objects);
133 d1cda826 2017-11-06 stsp return err;
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)
140 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
141 a1fd68d8 2018-01-12 stsp char *path;
143 4796fb13 2018-12-23 stsp err = object_path(&path, id, repo);
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();
152 4558fcd4 2018-01-14 stsp free(path);
153 2090a03d 2018-09-09 stsp return err;
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)
159 2090a03d 2018-09-09 stsp size_t size;
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);
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();
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);
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);
178 2090a03d 2018-09-09 stsp return NULL;
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)
184 a158c901 2018-12-23 stsp close(imsg_fds[0]);
186 a158c901 2018-12-23 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
187 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s\n", getprogname(),
188 a158c901 2018-12-23 stsp strerror(errno));
191 a158c901 2018-12-23 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
192 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s\n", getprogname(),
193 a158c901 2018-12-23 stsp strerror(errno));
197 a158c901 2018-12-23 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
198 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
199 a158c901 2018-12-23 stsp strerror(errno));
204 2090a03d 2018-09-09 stsp static const struct got_error *
205 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
206 a158c901 2018-12-23 stsp struct got_object_id *id)
208 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
209 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
211 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
213 a158c901 2018-12-23 stsp return err;
215 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
217 a158c901 2018-12-23 stsp return err;
219 a158c901 2018-12-23 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
220 a158c901 2018-12-23 stsp if ((*obj)->path_packfile == NULL) {
221 a158c901 2018-12-23 stsp err = got_error_from_errno();
222 a158c901 2018-12-23 stsp return err;
224 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
226 a158c901 2018-12-23 stsp return NULL;
229 a158c901 2018-12-23 stsp static const struct got_error *
230 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
232 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
233 a158c901 2018-12-23 stsp int imsg_fds[2];
235 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
237 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
238 a158c901 2018-12-23 stsp if (ibuf == NULL)
239 a158c901 2018-12-23 stsp return got_error_from_errno();
241 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
242 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
243 a158c901 2018-12-23 stsp err = got_error_from_errno();
244 a158c901 2018-12-23 stsp free(ibuf);
245 a158c901 2018-12-23 stsp return err;
248 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
249 a158c901 2018-12-23 stsp err = got_error_from_errno();
253 a158c901 2018-12-23 stsp pid = fork();
254 a158c901 2018-12-23 stsp if (pid == -1) {
255 a158c901 2018-12-23 stsp err = got_error_from_errno();
257 a158c901 2018-12-23 stsp } else if (pid == 0) {
258 a158c901 2018-12-23 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
259 a158c901 2018-12-23 stsp pack->path_packfile);
260 a158c901 2018-12-23 stsp /* not reached */
263 a158c901 2018-12-23 stsp close(imsg_fds[1]);
264 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
265 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
266 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
267 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
269 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
271 a158c901 2018-12-23 stsp const struct got_error *child_err;
272 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
273 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
274 a158c901 2018-12-23 stsp pack->privsep_child->pid);
275 a158c901 2018-12-23 stsp if (child_err && err == NULL)
276 a158c901 2018-12-23 stsp err = child_err;
280 a158c901 2018-12-23 stsp free(ibuf);
281 a158c901 2018-12-23 stsp free(pack->privsep_child);
282 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
284 a158c901 2018-12-23 stsp return err;
287 711fb6e8 2018-12-23 stsp static const struct got_error *
288 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
289 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
290 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
292 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
294 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
295 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
297 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
299 711fb6e8 2018-12-23 stsp return err;
301 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
305 a158c901 2018-12-23 stsp static const struct got_error *
306 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
307 2090a03d 2018-09-09 stsp struct got_repository *repo)
309 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
310 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
311 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
313 2090a03d 2018-09-09 stsp char *path_packfile;
315 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
317 2090a03d 2018-09-09 stsp return err;
319 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
321 2090a03d 2018-09-09 stsp return err;
323 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
324 2090a03d 2018-09-09 stsp if (pack == NULL) {
325 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
330 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
334 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
336 2090a03d 2018-09-09 stsp free(path_packfile);
337 4558fcd4 2018-01-14 stsp return err;
340 9f2369b0 2018-12-24 stsp static const struct got_error *
341 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
343 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
344 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
346 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
348 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
350 9f2369b0 2018-12-24 stsp return err;
352 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
355 9f2369b0 2018-12-24 stsp static const struct got_error *
356 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
357 9f2369b0 2018-12-24 stsp int obj_fd)
359 9f2369b0 2018-12-24 stsp int imsg_fds[2];
361 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
363 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
364 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
366 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
367 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
368 9f2369b0 2018-12-24 stsp return got_error_from_errno();
370 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
371 9f2369b0 2018-12-24 stsp return got_error_from_errno();
373 9f2369b0 2018-12-24 stsp pid = fork();
374 9f2369b0 2018-12-24 stsp if (pid == -1)
375 9f2369b0 2018-12-24 stsp return got_error_from_errno();
376 9f2369b0 2018-12-24 stsp else if (pid == 0) {
377 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
378 9f2369b0 2018-12-24 stsp repo->path);
379 9f2369b0 2018-12-24 stsp /* not reached */
382 9f2369b0 2018-12-24 stsp close(imsg_fds[1]);
383 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
384 9f2369b0 2018-12-24 stsp imsg_fds[0];
385 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
386 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
387 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
389 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
393 4558fcd4 2018-01-14 stsp const struct got_error *
394 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
395 4558fcd4 2018-01-14 stsp struct got_object_id *id)
397 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
398 6c00b545 2018-01-17 stsp char *path;
401 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
402 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
403 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
404 7bb0daa1 2018-06-21 stsp return NULL;
407 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
408 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
409 59790a32 2018-09-15 stsp return err;
410 59790a32 2018-09-15 stsp if (*obj) {
411 59790a32 2018-09-15 stsp (*obj)->refcnt++;
412 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
415 6c00b545 2018-01-17 stsp err = object_path(&path, id, repo);
417 4558fcd4 2018-01-14 stsp return err;
419 2178c42e 2018-04-22 stsp fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
420 2178c42e 2018-04-22 stsp if (fd == -1) {
421 59790a32 2018-09-15 stsp if (errno == ENOENT)
422 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
424 6c00b545 2018-01-17 stsp err = got_error_from_errno();
427 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
430 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
433 59790a32 2018-09-15 stsp (*obj)->refcnt++;
434 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
436 6c00b545 2018-01-17 stsp free(path);
437 2178c42e 2018-04-22 stsp if (fd != -1)
439 ab9a70b2 2017-11-06 stsp return err;
443 6dfa2fd3 2018-02-12 stsp const struct got_error *
444 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
445 6dfa2fd3 2018-02-12 stsp const char *id_str)
447 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
449 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
450 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
452 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
455 15a94983 2018-12-23 stsp const struct got_error *
456 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
457 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
459 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
460 15a94983 2018-12-23 stsp struct got_object *obj;
462 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
464 15a94983 2018-12-23 stsp return err;
466 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
467 15a94983 2018-12-23 stsp got_object_close(obj);
468 15a94983 2018-12-23 stsp if (*id == NULL)
469 15a94983 2018-12-23 stsp return got_error_from_errno();
471 15a94983 2018-12-23 stsp return NULL;
474 434025f3 2018-09-16 stsp static const struct got_error *
475 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
476 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
478 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
480 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
483 a158c901 2018-12-23 stsp return err;
485 a158c901 2018-12-23 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
488 a158c901 2018-12-23 stsp static const struct got_error *
489 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
490 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
491 a158c901 2018-12-23 stsp struct got_object_id *id)
493 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
495 a158c901 2018-12-23 stsp if (pack->privsep_child)
496 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
498 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
500 a158c901 2018-12-23 stsp return err;
502 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
505 9f2369b0 2018-12-24 stsp static const struct got_error *
506 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
509 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
510 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
512 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
514 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
516 9f2369b0 2018-12-24 stsp return err;
518 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
521 9f2369b0 2018-12-24 stsp static const struct got_error *
522 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
523 9f2369b0 2018-12-24 stsp struct got_repository *repo)
525 9f2369b0 2018-12-24 stsp int imsg_fds[2];
527 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
529 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
530 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
532 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
533 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
534 9f2369b0 2018-12-24 stsp return got_error_from_errno();
536 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
537 9f2369b0 2018-12-24 stsp return got_error_from_errno();
539 9f2369b0 2018-12-24 stsp pid = fork();
540 9f2369b0 2018-12-24 stsp if (pid == -1)
541 9f2369b0 2018-12-24 stsp return got_error_from_errno();
542 9f2369b0 2018-12-24 stsp else if (pid == 0) {
543 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
544 9f2369b0 2018-12-24 stsp repo->path);
545 9f2369b0 2018-12-24 stsp /* not reached */
548 9f2369b0 2018-12-24 stsp close(imsg_fds[1]);
549 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
550 9f2369b0 2018-12-24 stsp imsg_fds[0];
551 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
552 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
553 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
555 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
559 a158c901 2018-12-23 stsp static const struct got_error *
560 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
561 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
563 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
564 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
566 1785f84a 2018-12-23 stsp char *path_packfile;
568 434025f3 2018-09-16 stsp if (check_cache) {
569 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
570 434025f3 2018-09-16 stsp if (*commit != NULL) {
571 434025f3 2018-09-16 stsp (*commit)->refcnt++;
572 434025f3 2018-09-16 stsp return NULL;
575 434025f3 2018-09-16 stsp *commit = NULL;
577 1785f84a 2018-12-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
578 1785f84a 2018-12-23 stsp if (err == NULL) {
579 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
581 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
583 1785f84a 2018-12-23 stsp return err;
585 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
586 434025f3 2018-09-16 stsp if (pack == NULL) {
587 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
590 434025f3 2018-09-16 stsp return err;
592 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
593 1785f84a 2018-12-23 stsp packidx, idx, id);
594 1785f84a 2018-12-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
597 1785f84a 2018-12-23 stsp err = open_loose_object(&fd, id, repo);
599 434025f3 2018-09-16 stsp return err;
600 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
604 434025f3 2018-09-16 stsp if (err == NULL) {
605 434025f3 2018-09-16 stsp (*commit)->refcnt++;
606 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
609 e32baab7 2018-11-05 stsp return err;
612 e32baab7 2018-11-05 stsp const struct got_error *
613 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
614 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
616 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
617 e32baab7 2018-11-05 stsp if (*commit != NULL) {
618 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
619 e32baab7 2018-11-05 stsp return NULL;
622 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
625 e32baab7 2018-11-05 stsp const struct got_error *
626 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
627 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
629 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
632 434025f3 2018-09-16 stsp const struct got_error *
633 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
635 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
637 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
638 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
639 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
641 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
642 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
643 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
644 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
645 dbc6a6b6 2018-07-12 stsp *qid = NULL;
646 dbc6a6b6 2018-07-12 stsp return err;
649 dbc6a6b6 2018-07-12 stsp return NULL;
652 a158c901 2018-12-23 stsp static const struct got_error *
653 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
654 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
656 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
658 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
661 a158c901 2018-12-23 stsp return err;
663 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
666 71eb0e7f 2018-09-16 stsp static const struct got_error *
667 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
668 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
669 13c729f7 2018-12-24 stsp struct got_object_id *id)
671 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
673 13c729f7 2018-12-24 stsp if (pack->privsep_child)
674 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
676 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
678 13c729f7 2018-12-24 stsp return err;
680 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
683 13c729f7 2018-12-24 stsp static const struct got_error *
684 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
687 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
688 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
690 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
692 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
694 9f2369b0 2018-12-24 stsp return err;
696 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
699 9f2369b0 2018-12-24 stsp const struct got_error *
700 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
701 9f2369b0 2018-12-24 stsp struct got_repository *repo)
703 9f2369b0 2018-12-24 stsp int imsg_fds[2];
705 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
707 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
708 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
710 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
711 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
712 9f2369b0 2018-12-24 stsp return got_error_from_errno();
714 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
715 9f2369b0 2018-12-24 stsp return got_error_from_errno();
717 9f2369b0 2018-12-24 stsp pid = fork();
718 9f2369b0 2018-12-24 stsp if (pid == -1)
719 9f2369b0 2018-12-24 stsp return got_error_from_errno();
720 9f2369b0 2018-12-24 stsp else if (pid == 0) {
721 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
722 9f2369b0 2018-12-24 stsp repo->path);
723 9f2369b0 2018-12-24 stsp /* not reached */
726 9f2369b0 2018-12-24 stsp close(imsg_fds[1]);
728 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
729 9f2369b0 2018-12-24 stsp imsg_fds[0];
730 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
731 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
732 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
735 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
738 9f2369b0 2018-12-24 stsp static const struct got_error *
739 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
740 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
742 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
743 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
745 13c729f7 2018-12-24 stsp char *path_packfile;
747 71eb0e7f 2018-09-16 stsp if (check_cache) {
748 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
749 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
750 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
751 71eb0e7f 2018-09-16 stsp return NULL;
754 71eb0e7f 2018-09-16 stsp *tree = NULL;
756 13c729f7 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
757 13c729f7 2018-12-24 stsp if (err == NULL) {
758 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
760 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
762 13c729f7 2018-12-24 stsp return err;
764 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
765 e7885405 2018-09-10 stsp if (pack == NULL) {
766 13c729f7 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
769 e7885405 2018-09-10 stsp return err;
771 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
772 13c729f7 2018-12-24 stsp packidx, idx, id);
773 13c729f7 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
776 13c729f7 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
778 e0ab43e7 2018-03-16 stsp return err;
779 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
783 f6be5c30 2018-06-22 stsp if (err == NULL) {
784 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
785 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
788 776d4d29 2018-06-17 stsp return err;
791 776d4d29 2018-06-17 stsp const struct got_error *
792 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
793 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
795 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
796 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
797 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
798 e8eb494a 2018-09-16 stsp return NULL;
801 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
804 71eb0e7f 2018-09-16 stsp const struct got_error *
805 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
806 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
808 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
811 883f0469 2018-06-23 stsp const struct got_tree_entries *
812 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
814 883f0469 2018-06-23 stsp return &tree->entries;
817 3840f4c9 2018-09-12 stsp static const struct got_error *
818 ebc55e2d 2018-12-24 stsp request_packed_blob(size_t *size, size_t *hdrlen, int outfd,
819 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
820 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
822 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
823 3840f4c9 2018-09-12 stsp int outfd_child;
824 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
826 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
827 3840f4c9 2018-09-12 stsp if (basefd == -1)
828 3840f4c9 2018-09-12 stsp return got_error_from_errno();
829 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
830 3840f4c9 2018-09-12 stsp if (accumfd == -1)
831 3840f4c9 2018-09-12 stsp return got_error_from_errno();
833 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
834 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
835 3840f4c9 2018-09-12 stsp return got_error_from_errno();
837 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
839 3840f4c9 2018-09-12 stsp return err;
841 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
842 3840f4c9 2018-09-12 stsp outfd_child);
844 3840f4c9 2018-09-12 stsp close(outfd_child);
845 3840f4c9 2018-09-12 stsp return err;
847 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
850 3840f4c9 2018-09-12 stsp close(basefd);
851 3840f4c9 2018-09-12 stsp close(accumfd);
852 3840f4c9 2018-09-12 stsp close(outfd_child);
853 3840f4c9 2018-09-12 stsp return err;
856 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
859 3840f4c9 2018-09-12 stsp close(accumfd);
860 3840f4c9 2018-09-12 stsp close(outfd_child);
861 3840f4c9 2018-09-12 stsp return err;
864 ebc55e2d 2018-12-24 stsp err = got_privsep_recv_blob(size, hdrlen,
865 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
867 3840f4c9 2018-09-12 stsp return err;
869 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
870 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
872 3840f4c9 2018-09-12 stsp return err;
875 ebc55e2d 2018-12-24 stsp static const struct got_error *
876 ebc55e2d 2018-12-24 stsp read_packed_blob_privsep(size_t *size, size_t *hdrlen, int outfd,
877 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
878 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
880 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
882 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
883 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
885 ebc55e2d 2018-12-24 stsp return err;
888 ebc55e2d 2018-12-24 stsp return request_packed_blob(size, hdrlen, outfd, pack, packidx, idx, id);
891 9f2369b0 2018-12-24 stsp static const struct got_error *
892 9f2369b0 2018-12-24 stsp request_blob(size_t *size, size_t *hdrlen, int outfd, int infd,
893 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf)
895 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
896 9f2369b0 2018-12-24 stsp int outfd_child;
898 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
899 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
900 9f2369b0 2018-12-24 stsp return got_error_from_errno();
902 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
904 9f2369b0 2018-12-24 stsp return err;
906 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
908 9f2369b0 2018-12-24 stsp close(outfd_child);
909 9f2369b0 2018-12-24 stsp return err;
912 9f2369b0 2018-12-24 stsp err = got_privsep_recv_blob(size, hdrlen, ibuf);
914 9f2369b0 2018-12-24 stsp return err;
916 9f2369b0 2018-12-24 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
917 9f2369b0 2018-12-24 stsp return got_error_from_errno();
919 9f2369b0 2018-12-24 stsp return err;
922 9f2369b0 2018-12-24 stsp static const struct got_error *
923 9f2369b0 2018-12-24 stsp read_blob_privsep(size_t *size, size_t *hdrlen,
924 9f2369b0 2018-12-24 stsp int outfd, int infd, struct got_repository *repo)
926 9f2369b0 2018-12-24 stsp int imsg_fds[2];
928 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
930 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd != -1) {
931 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf;
932 9f2369b0 2018-12-24 stsp return request_blob(size, hdrlen, outfd, infd, ibuf);
935 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
936 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
937 9f2369b0 2018-12-24 stsp return got_error_from_errno();
939 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
940 9f2369b0 2018-12-24 stsp return got_error_from_errno();
942 9f2369b0 2018-12-24 stsp pid = fork();
943 9f2369b0 2018-12-24 stsp if (pid == -1)
944 9f2369b0 2018-12-24 stsp return got_error_from_errno();
945 9f2369b0 2018-12-24 stsp else if (pid == 0) {
946 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_BLOB,
947 9f2369b0 2018-12-24 stsp repo->path);
948 9f2369b0 2018-12-24 stsp /* not reached */
951 9f2369b0 2018-12-24 stsp close(imsg_fds[1]);
952 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
953 9f2369b0 2018-12-24 stsp imsg_fds[0];
954 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
955 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
956 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
958 9f2369b0 2018-12-24 stsp return request_blob(size, hdrlen, outfd, infd, ibuf);
961 ebc55e2d 2018-12-24 stsp static const struct got_error *
962 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
963 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
965 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
966 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
968 ebc55e2d 2018-12-24 stsp char *path_packfile;
970 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
971 ebc55e2d 2018-12-24 stsp struct stat sb;
973 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
974 4558fcd4 2018-01-14 stsp if (*blob == NULL)
975 0a585a0d 2018-03-17 stsp return got_error_from_errno();
977 55da3778 2018-09-10 stsp outfd = got_opentempfd();
978 55da3778 2018-09-10 stsp if (outfd == -1)
979 55da3778 2018-09-10 stsp return got_error_from_errno();
981 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
982 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
983 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
987 ebc55e2d 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
988 ebc55e2d 2018-12-24 stsp if (err == NULL) {
989 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
991 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
995 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
996 55da3778 2018-09-10 stsp if (pack == NULL) {
997 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1000 55da3778 2018-09-10 stsp goto done;
1002 ebc55e2d 2018-12-24 stsp err = read_packed_blob_privsep(&size, &hdrlen, outfd, pack,
1003 ebc55e2d 2018-12-24 stsp packidx, idx, id);
1004 ebc55e2d 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1007 ebc55e2d 2018-12-24 stsp err = open_loose_object(&infd, id, repo);
1009 c7254d79 2018-04-24 stsp goto done;
1010 9f2369b0 2018-12-24 stsp err = read_blob_privsep(&size, &hdrlen, outfd, infd, repo);
1011 ebc55e2d 2018-12-24 stsp close(infd);
1014 ebc55e2d 2018-12-24 stsp goto done;
1016 55da3778 2018-09-10 stsp if (fstat(outfd, &sb) == -1) {
1017 55da3778 2018-09-10 stsp err = got_error_from_errno();
1018 55da3778 2018-09-10 stsp goto done;
1021 ebc55e2d 2018-12-24 stsp if (sb.st_size != size) {
1022 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1023 55da3778 2018-09-10 stsp goto done;
1026 de060dff 2018-12-24 stsp if (hdrlen > size) {
1027 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1028 ebc55e2d 2018-12-24 stsp goto done;
1031 55da3778 2018-09-10 stsp (*blob)->f = fdopen(outfd, "rb");
1032 55da3778 2018-09-10 stsp if ((*blob)->f == NULL) {
1033 55da3778 2018-09-10 stsp err = got_error_from_errno();
1034 55da3778 2018-09-10 stsp close(outfd);
1035 55da3778 2018-09-10 stsp goto done;
1038 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1039 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1040 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1043 55da3778 2018-09-10 stsp if (err) {
1044 55da3778 2018-09-10 stsp if (*blob) {
1045 55da3778 2018-09-10 stsp if ((*blob)->f)
1046 55da3778 2018-09-10 stsp fclose((*blob)->f);
1047 55da3778 2018-09-10 stsp free((*blob)->read_buf);
1048 55da3778 2018-09-10 stsp free(*blob);
1049 55da3778 2018-09-10 stsp *blob = NULL;
1050 55da3778 2018-09-10 stsp } else if (outfd != -1)
1051 55da3778 2018-09-10 stsp close(outfd);
1053 a19581a2 2018-06-21 stsp return err;
1056 a19581a2 2018-06-21 stsp const struct got_error *
1057 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1058 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1059 a19581a2 2018-06-21 stsp size_t blocksize)
1061 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1064 ebc55e2d 2018-12-24 stsp const struct got_error *
1065 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1066 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1068 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1072 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1074 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1075 68482ea3 2017-11-27 stsp fclose(blob->f);
1076 68482ea3 2017-11-27 stsp free(blob);
1080 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1082 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1086 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1088 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1091 f934cf2c 2018-02-12 stsp const uint8_t *
1092 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1094 f934cf2c 2018-02-12 stsp return blob->read_buf;
1097 68482ea3 2017-11-27 stsp const struct got_error *
1098 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1102 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1103 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1104 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1105 eb651edf 2018-02-11 stsp *outlenp = n;
1106 35e9ba5d 2018-06-21 stsp return NULL;
1109 35e9ba5d 2018-06-21 stsp const struct got_error *
1110 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1111 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
1113 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1114 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1115 84451b3e 2018-07-10 stsp const uint8_t *buf;
1118 84451b3e 2018-07-10 stsp if (total_len)
1119 84451b3e 2018-07-10 stsp *total_len = 0;
1120 84451b3e 2018-07-10 stsp if (nlines)
1121 84451b3e 2018-07-10 stsp *nlines = 0;
1123 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1125 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1127 35e9ba5d 2018-06-21 stsp return err;
1128 35e9ba5d 2018-06-21 stsp if (len == 0)
1130 84451b3e 2018-07-10 stsp if (total_len)
1131 84451b3e 2018-07-10 stsp *total_len += len;
1132 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1133 84451b3e 2018-07-10 stsp if (nlines) {
1134 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
1135 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
1136 84451b3e 2018-07-10 stsp (*nlines)++;
1139 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1140 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1141 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1142 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1143 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1144 35e9ba5d 2018-06-21 stsp } while (len != 0);
1146 35e9ba5d 2018-06-21 stsp fflush(outfile);
1147 35e9ba5d 2018-06-21 stsp rewind(outfile);
1149 776d4d29 2018-06-17 stsp return NULL;
1152 f4a881ce 2018-11-17 stsp static const struct got_error *
1153 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1154 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1156 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1158 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1159 268f7291 2018-12-24 stsp pack_idx);
1161 a158c901 2018-12-23 stsp return err;
1163 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1166 268f7291 2018-12-24 stsp static const struct got_error *
1167 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1168 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1169 268f7291 2018-12-24 stsp struct got_object_id *id)
1171 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1173 268f7291 2018-12-24 stsp if (pack->privsep_child)
1174 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1176 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1178 268f7291 2018-12-24 stsp return err;
1180 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1183 9f2369b0 2018-12-24 stsp static const struct got_error *
1184 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1187 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1188 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1190 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1192 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1194 9f2369b0 2018-12-24 stsp return err;
1196 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1199 9f2369b0 2018-12-24 stsp static const struct got_error *
1200 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1201 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1203 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1204 9f2369b0 2018-12-24 stsp pid_t pid;
1205 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1207 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1208 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1210 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1211 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1212 9f2369b0 2018-12-24 stsp return got_error_from_errno();
1214 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1215 9f2369b0 2018-12-24 stsp return got_error_from_errno();
1217 9f2369b0 2018-12-24 stsp pid = fork();
1218 9f2369b0 2018-12-24 stsp if (pid == -1)
1219 9f2369b0 2018-12-24 stsp return got_error_from_errno();
1220 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1221 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1222 9f2369b0 2018-12-24 stsp repo->path);
1223 9f2369b0 2018-12-24 stsp /* not reached */
1226 9f2369b0 2018-12-24 stsp close(imsg_fds[1]);
1227 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1228 9f2369b0 2018-12-24 stsp imsg_fds[0];
1229 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1230 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1231 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1233 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1236 a158c901 2018-12-23 stsp static const struct got_error *
1237 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1238 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1240 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1241 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1243 268f7291 2018-12-24 stsp char *path_packfile;
1245 f4a881ce 2018-11-17 stsp if (check_cache) {
1246 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1247 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1248 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1249 f4a881ce 2018-11-17 stsp return NULL;
1252 f4a881ce 2018-11-17 stsp *tag = NULL;
1254 268f7291 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1255 268f7291 2018-12-24 stsp if (err == NULL) {
1256 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1258 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1260 268f7291 2018-12-24 stsp return err;
1262 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1263 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1264 268f7291 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1267 f4a881ce 2018-11-17 stsp return err;
1269 268f7291 2018-12-24 stsp err = read_packed_tag_privsep(tag, pack,
1270 268f7291 2018-12-24 stsp packidx, idx, id);
1271 268f7291 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1274 268f7291 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
1276 f4a881ce 2018-11-17 stsp return err;
1277 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1278 f4a881ce 2018-11-17 stsp close(fd);
1281 f4a881ce 2018-11-17 stsp if (err == NULL) {
1282 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1283 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1286 f4a881ce 2018-11-17 stsp return err;
1289 f4a881ce 2018-11-17 stsp const struct got_error *
1290 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1291 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1293 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1294 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1295 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1296 f4a881ce 2018-11-17 stsp return NULL;
1299 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1302 f4a881ce 2018-11-17 stsp const struct got_error *
1303 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1304 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1306 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1309 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1310 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1312 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1314 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1315 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1316 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1317 63da309a 2018-11-07 stsp if (cmp < 0)
1319 63da309a 2018-11-07 stsp if (cmp > 0)
1321 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1322 776d4d29 2018-06-17 stsp return te;
1324 eb651edf 2018-02-11 stsp return NULL;
1327 776d4d29 2018-06-17 stsp const struct got_error *
1328 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1329 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1331 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1332 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1333 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1334 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1335 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1336 b7cd37e5 2018-11-18 stsp size_t seglen;
1338 27d434c2 2018-09-15 stsp *id = NULL;
1340 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1341 776d4d29 2018-06-17 stsp if (path[0] != '/')
1342 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1344 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1346 776d4d29 2018-06-17 stsp goto done;
1348 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1349 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1350 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1351 27d434c2 2018-09-15 stsp if (*id == NULL)
1352 27d434c2 2018-09-15 stsp err = got_error_from_errno();
1353 ca008b32 2018-07-22 stsp goto done;
1356 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1358 776d4d29 2018-06-17 stsp goto done;
1361 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1363 65a9bbe9 2018-09-15 stsp seglen = 0;
1364 b7cd37e5 2018-11-18 stsp while (*s) {
1365 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1367 776d4d29 2018-06-17 stsp if (*s != '/') {
1374 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1375 db37e2c0 2018-06-21 stsp if (te == NULL) {
1376 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1377 776d4d29 2018-06-17 stsp goto done;
1380 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1383 776d4d29 2018-06-17 stsp seg = s + 1;
1384 65a9bbe9 2018-09-15 stsp seglen = 0;
1387 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1389 db37e2c0 2018-06-21 stsp te = NULL;
1391 776d4d29 2018-06-17 stsp goto done;
1392 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1393 776d4d29 2018-06-17 stsp tree = next_tree;
1398 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1399 27d434c2 2018-09-15 stsp if (*id == NULL)
1400 27d434c2 2018-09-15 stsp return got_error_from_errno();
1402 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1404 776d4d29 2018-06-17 stsp if (commit)
1405 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1407 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1408 776d4d29 2018-06-17 stsp return err;
1411 07862c20 2018-09-15 stsp const struct got_error *
1412 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1413 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1414 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1416 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1417 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1418 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1419 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1420 3b7f9878 2018-11-18 stsp size_t seglen;
1422 07862c20 2018-09-15 stsp *changed = 0;
1424 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1425 07862c20 2018-09-15 stsp if (path[0] != '/')
1426 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1428 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1429 07862c20 2018-09-15 stsp if (path[1] == '\0')
1430 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1432 07862c20 2018-09-15 stsp tree1 = tree01;
1433 07862c20 2018-09-15 stsp tree2 = tree02;
1435 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1437 65a9bbe9 2018-09-15 stsp seglen = 0;
1438 3b7f9878 2018-11-18 stsp while (*s) {
1439 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1441 07862c20 2018-09-15 stsp if (*s != '/') {
1448 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1449 07862c20 2018-09-15 stsp if (te1 == NULL) {
1450 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1451 07862c20 2018-09-15 stsp goto done;
1454 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1455 07862c20 2018-09-15 stsp if (te2 == NULL) {
1456 07862c20 2018-09-15 stsp *changed = 1;
1457 07862c20 2018-09-15 stsp goto done;
1460 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1461 07862c20 2018-09-15 stsp *changed = 1;
1462 07862c20 2018-09-15 stsp goto done;
1465 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1466 07862c20 2018-09-15 stsp *changed = 0;
1467 07862c20 2018-09-15 stsp goto done;
1470 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1471 07862c20 2018-09-15 stsp *changed = 1;
1472 07862c20 2018-09-15 stsp goto done;
1475 07862c20 2018-09-15 stsp seg = s + 1;
1477 65a9bbe9 2018-09-15 stsp seglen = 0;
1479 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1481 07862c20 2018-09-15 stsp te1 = NULL;
1483 07862c20 2018-09-15 stsp goto done;
1484 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1485 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1486 07862c20 2018-09-15 stsp tree1 = next_tree1;
1488 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1490 07862c20 2018-09-15 stsp te2 = NULL;
1492 07862c20 2018-09-15 stsp goto done;
1493 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1494 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1495 07862c20 2018-09-15 stsp tree2 = next_tree2;
1499 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1500 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1501 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1502 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1503 77880158 2018-11-04 stsp return err;