2 5d56da81 2019-01-13 stsp * Copyright (c) 2018, 2019 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 90bdb554 2019-04-11 stsp const struct got_error *
111 90bdb554 2019-04-11 stsp got_object_get_path(char **path, struct got_object_id *id,
112 90bdb554 2019-04-11 stsp struct got_repository *repo)
114 ab9a70b2 2017-11-06 stsp const struct got_error *err = NULL;
115 7a132809 2018-07-23 stsp char *hex = NULL;
116 d1cda826 2017-11-06 stsp char *path_objects = got_repo_get_path_objects(repo);
118 e6b1056e 2018-04-22 stsp *path = NULL;
120 ab9a70b2 2017-11-06 stsp if (path_objects == NULL)
121 0a585a0d 2018-03-17 stsp return got_error_from_errno();
123 ef0981d5 2018-02-12 stsp err = got_object_id_str(&hex, id);
127 d1cda826 2017-11-06 stsp if (asprintf(path, "%s/%.2x/%s", path_objects,
128 d1cda826 2017-11-06 stsp id->sha1[0], hex + 2) == -1)
129 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
133 d1cda826 2017-11-06 stsp free(path_objects);
134 d1cda826 2017-11-06 stsp return err;
137 4ee4114f 2018-01-23 stsp static const struct got_error *
138 4796fb13 2018-12-23 stsp open_loose_object(int *fd, struct got_object_id *id,
139 4796fb13 2018-12-23 stsp struct got_repository *repo)
141 d1cda826 2017-11-06 stsp const struct got_error *err = NULL;
142 a1fd68d8 2018-01-12 stsp char *path;
144 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
146 d1cda826 2017-11-06 stsp return err;
147 a5b57ccf 2019-04-11 stsp *fd = open(path, O_RDONLY | O_NOFOLLOW);
148 d5003b79 2018-04-22 stsp if (*fd == -1) {
149 6c00b545 2018-01-17 stsp err = got_error_from_errno();
153 4558fcd4 2018-01-14 stsp free(path);
154 2090a03d 2018-09-09 stsp return err;
157 2090a03d 2018-09-09 stsp static const struct got_error *
158 2090a03d 2018-09-09 stsp get_packfile_path(char **path_packfile, struct got_packidx *packidx)
160 2090a03d 2018-09-09 stsp size_t size;
162 2090a03d 2018-09-09 stsp /* Packfile path contains ".pack" instead of ".idx", so add one byte. */
163 2090a03d 2018-09-09 stsp size = strlen(packidx->path_packidx) + 2;
164 2090a03d 2018-09-09 stsp if (size < GOT_PACKFILE_NAMELEN + 1)
165 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_BAD_PATH);
167 08451938 2018-11-05 stsp *path_packfile = malloc(size);
168 2090a03d 2018-09-09 stsp if (*path_packfile == NULL)
169 2090a03d 2018-09-09 stsp return got_error_from_errno();
171 2090a03d 2018-09-09 stsp /* Copy up to and excluding ".idx". */
172 2090a03d 2018-09-09 stsp if (strlcpy(*path_packfile, packidx->path_packidx,
173 2090a03d 2018-09-09 stsp size - strlen(GOT_PACKIDX_SUFFIX) - 1) >= size)
174 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
176 2090a03d 2018-09-09 stsp if (strlcat(*path_packfile, GOT_PACKFILE_SUFFIX, size) >= size)
177 2090a03d 2018-09-09 stsp return got_error(GOT_ERR_NO_SPACE);
179 2090a03d 2018-09-09 stsp return NULL;
182 a158c901 2018-12-23 stsp static void
183 a158c901 2018-12-23 stsp exec_privsep_child(int imsg_fds[2], const char *path, const char *repo_path)
185 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[0]) != 0) {
186 3a6ce05a 2019-02-11 stsp fprintf(stderr, "%s: %s\n", getprogname(),
187 3a6ce05a 2019-02-11 stsp strerror(errno));
191 a158c901 2018-12-23 stsp if (dup2(imsg_fds[1], GOT_IMSG_FD_CHILD) == -1) {
192 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s\n", getprogname(),
193 a158c901 2018-12-23 stsp strerror(errno));
196 a158c901 2018-12-23 stsp if (closefrom(GOT_IMSG_FD_CHILD + 1) == -1) {
197 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s\n", getprogname(),
198 a158c901 2018-12-23 stsp strerror(errno));
202 a158c901 2018-12-23 stsp if (execl(path, path, repo_path, (char *)NULL) == -1) {
203 a158c901 2018-12-23 stsp fprintf(stderr, "%s: %s: %s\n", getprogname(), path,
204 a158c901 2018-12-23 stsp strerror(errno));
209 2090a03d 2018-09-09 stsp static const struct got_error *
210 a158c901 2018-12-23 stsp request_packed_object(struct got_object **obj, struct got_pack *pack, int idx,
211 a158c901 2018-12-23 stsp struct got_object_id *id)
213 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
214 a158c901 2018-12-23 stsp struct imsgbuf *ibuf = pack->privsep_child->ibuf;
216 a158c901 2018-12-23 stsp err = got_privsep_send_packed_obj_req(ibuf, idx, id);
218 a158c901 2018-12-23 stsp return err;
220 a158c901 2018-12-23 stsp err = got_privsep_recv_obj(obj, ibuf);
222 a158c901 2018-12-23 stsp return err;
224 a158c901 2018-12-23 stsp (*obj)->path_packfile = strdup(pack->path_packfile);
225 a158c901 2018-12-23 stsp if ((*obj)->path_packfile == NULL) {
226 a158c901 2018-12-23 stsp err = got_error_from_errno();
227 a158c901 2018-12-23 stsp return err;
229 a158c901 2018-12-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
231 a158c901 2018-12-23 stsp return NULL;
234 a158c901 2018-12-23 stsp static const struct got_error *
235 711fb6e8 2018-12-23 stsp start_pack_privsep_child(struct got_pack *pack, struct got_packidx *packidx)
237 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
238 a158c901 2018-12-23 stsp int imsg_fds[2];
240 a158c901 2018-12-23 stsp struct imsgbuf *ibuf;
242 a158c901 2018-12-23 stsp ibuf = calloc(1, sizeof(*ibuf));
243 a158c901 2018-12-23 stsp if (ibuf == NULL)
244 a158c901 2018-12-23 stsp return got_error_from_errno();
246 a158c901 2018-12-23 stsp pack->privsep_child = calloc(1, sizeof(*pack->privsep_child));
247 a158c901 2018-12-23 stsp if (pack->privsep_child == NULL) {
248 a158c901 2018-12-23 stsp err = got_error_from_errno();
249 a158c901 2018-12-23 stsp free(ibuf);
250 a158c901 2018-12-23 stsp return err;
253 a158c901 2018-12-23 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1) {
254 a158c901 2018-12-23 stsp err = got_error_from_errno();
258 a158c901 2018-12-23 stsp pid = fork();
259 a158c901 2018-12-23 stsp if (pid == -1) {
260 a158c901 2018-12-23 stsp err = got_error_from_errno();
262 a158c901 2018-12-23 stsp } else if (pid == 0) {
263 a158c901 2018-12-23 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_PACK,
264 a158c901 2018-12-23 stsp pack->path_packfile);
265 a158c901 2018-12-23 stsp /* not reached */
268 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
269 3a6ce05a 2019-02-11 stsp return got_error_from_errno();
270 a158c901 2018-12-23 stsp pack->privsep_child->imsg_fd = imsg_fds[0];
271 a158c901 2018-12-23 stsp pack->privsep_child->pid = pid;
272 a158c901 2018-12-23 stsp imsg_init(ibuf, imsg_fds[0]);
273 a158c901 2018-12-23 stsp pack->privsep_child->ibuf = ibuf;
275 a158c901 2018-12-23 stsp err = got_privsep_init_pack_child(ibuf, pack, packidx);
277 a158c901 2018-12-23 stsp const struct got_error *child_err;
278 a158c901 2018-12-23 stsp err = got_privsep_send_stop(pack->privsep_child->imsg_fd);
279 a158c901 2018-12-23 stsp child_err = got_privsep_wait_for_child(
280 a158c901 2018-12-23 stsp pack->privsep_child->pid);
281 a158c901 2018-12-23 stsp if (child_err && err == NULL)
282 a158c901 2018-12-23 stsp err = child_err;
286 a158c901 2018-12-23 stsp free(ibuf);
287 a158c901 2018-12-23 stsp free(pack->privsep_child);
288 a158c901 2018-12-23 stsp pack->privsep_child = NULL;
290 a158c901 2018-12-23 stsp return err;
293 711fb6e8 2018-12-23 stsp static const struct got_error *
294 711fb6e8 2018-12-23 stsp read_packed_object_privsep(struct got_object **obj,
295 711fb6e8 2018-12-23 stsp struct got_repository *repo, struct got_pack *pack,
296 711fb6e8 2018-12-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id)
298 711fb6e8 2018-12-23 stsp const struct got_error *err = NULL;
300 711fb6e8 2018-12-23 stsp if (pack->privsep_child)
301 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
303 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
305 711fb6e8 2018-12-23 stsp return err;
307 711fb6e8 2018-12-23 stsp return request_packed_object(obj, pack, idx, id);
311 a158c901 2018-12-23 stsp static const struct got_error *
312 2090a03d 2018-09-09 stsp open_packed_object(struct got_object **obj, struct got_object_id *id,
313 2090a03d 2018-09-09 stsp struct got_repository *repo)
315 2090a03d 2018-09-09 stsp const struct got_error *err = NULL;
316 2090a03d 2018-09-09 stsp struct got_pack *pack = NULL;
317 2090a03d 2018-09-09 stsp struct got_packidx *packidx = NULL;
319 2090a03d 2018-09-09 stsp char *path_packfile;
321 2090a03d 2018-09-09 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
323 2090a03d 2018-09-09 stsp return err;
325 2090a03d 2018-09-09 stsp err = get_packfile_path(&path_packfile, packidx);
327 2090a03d 2018-09-09 stsp return err;
329 2090a03d 2018-09-09 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
330 2090a03d 2018-09-09 stsp if (pack == NULL) {
331 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
336 a158c901 2018-12-23 stsp err = read_packed_object_privsep(obj, repo, pack, packidx, idx, id);
340 2090a03d 2018-09-09 stsp err = got_repo_cache_pack(NULL, repo, (*obj)->path_packfile, packidx);
342 2090a03d 2018-09-09 stsp free(path_packfile);
343 4558fcd4 2018-01-14 stsp return err;
346 9f2369b0 2018-12-24 stsp static const struct got_error *
347 9f2369b0 2018-12-24 stsp request_object(struct got_object **obj, struct got_repository *repo, int fd)
349 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
350 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
352 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf;
354 aea5f015 2018-12-24 stsp err = got_privsep_send_obj_req(ibuf, fd);
356 9f2369b0 2018-12-24 stsp return err;
358 9f2369b0 2018-12-24 stsp return got_privsep_recv_obj(obj, ibuf);
361 9f2369b0 2018-12-24 stsp static const struct got_error *
362 9f2369b0 2018-12-24 stsp read_object_header_privsep(struct got_object **obj, struct got_repository *repo,
363 9f2369b0 2018-12-24 stsp int obj_fd)
365 9f2369b0 2018-12-24 stsp int imsg_fds[2];
367 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
369 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd != -1)
370 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
372 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
373 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
374 9f2369b0 2018-12-24 stsp return got_error_from_errno();
376 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
377 9f2369b0 2018-12-24 stsp return got_error_from_errno();
379 9f2369b0 2018-12-24 stsp pid = fork();
380 9f2369b0 2018-12-24 stsp if (pid == -1)
381 9f2369b0 2018-12-24 stsp return got_error_from_errno();
382 9f2369b0 2018-12-24 stsp else if (pid == 0) {
383 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_OBJECT,
384 9f2369b0 2018-12-24 stsp repo->path);
385 9f2369b0 2018-12-24 stsp /* not reached */
388 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
389 3a6ce05a 2019-02-11 stsp return got_error_from_errno();
390 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].imsg_fd =
391 9f2369b0 2018-12-24 stsp imsg_fds[0];
392 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].pid = pid;
393 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
394 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_OBJECT].ibuf = ibuf;
396 9f2369b0 2018-12-24 stsp return request_object(obj, repo, obj_fd);
400 4558fcd4 2018-01-14 stsp const struct got_error *
401 4558fcd4 2018-01-14 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
402 4558fcd4 2018-01-14 stsp struct got_object_id *id)
404 6c00b545 2018-01-17 stsp const struct got_error *err = NULL;
405 6c00b545 2018-01-17 stsp char *path;
408 7bb0daa1 2018-06-21 stsp *obj = got_repo_get_cached_object(repo, id);
409 7bb0daa1 2018-06-21 stsp if (*obj != NULL) {
410 7bb0daa1 2018-06-21 stsp (*obj)->refcnt++;
411 7bb0daa1 2018-06-21 stsp return NULL;
414 59790a32 2018-09-15 stsp err = open_packed_object(obj, id, repo);
415 59790a32 2018-09-15 stsp if (err && err->code != GOT_ERR_NO_OBJ)
416 59790a32 2018-09-15 stsp return err;
417 59790a32 2018-09-15 stsp if (*obj) {
418 59790a32 2018-09-15 stsp (*obj)->refcnt++;
419 59790a32 2018-09-15 stsp return got_repo_cache_object(repo, id, *obj);
422 90bdb554 2019-04-11 stsp err = got_object_get_path(&path, id, repo);
424 4558fcd4 2018-01-14 stsp return err;
426 a5b57ccf 2019-04-11 stsp fd = open(path, O_RDONLY | O_NOFOLLOW);
427 2178c42e 2018-04-22 stsp if (fd == -1) {
428 59790a32 2018-09-15 stsp if (errno == ENOENT)
429 91a3d81f 2018-11-11 stsp err = got_error_no_obj(id);
431 6c00b545 2018-01-17 stsp err = got_error_from_errno();
434 9f2369b0 2018-12-24 stsp err = read_object_header_privsep(obj, repo, fd);
437 ab9a70b2 2017-11-06 stsp memcpy((*obj)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
440 59790a32 2018-09-15 stsp (*obj)->refcnt++;
441 59790a32 2018-09-15 stsp err = got_repo_cache_object(repo, id, *obj);
443 6c00b545 2018-01-17 stsp free(path);
444 ab9a70b2 2017-11-06 stsp return err;
448 6dfa2fd3 2018-02-12 stsp const struct got_error *
449 6dfa2fd3 2018-02-12 stsp got_object_open_by_id_str(struct got_object **obj, struct got_repository *repo,
450 6dfa2fd3 2018-02-12 stsp const char *id_str)
452 6dfa2fd3 2018-02-12 stsp struct got_object_id id;
454 6dfa2fd3 2018-02-12 stsp if (!got_parse_sha1_digest(id.sha1, id_str))
455 6dfa2fd3 2018-02-12 stsp return got_error(GOT_ERR_BAD_OBJ_ID_STR);
457 6dfa2fd3 2018-02-12 stsp return got_object_open(obj, repo, &id);
460 15a94983 2018-12-23 stsp const struct got_error *
461 15a94983 2018-12-23 stsp got_object_resolve_id_str(struct got_object_id **id,
462 15a94983 2018-12-23 stsp struct got_repository *repo, const char *id_str)
464 15a94983 2018-12-23 stsp const struct got_error *err = NULL;
465 15a94983 2018-12-23 stsp struct got_object *obj;
467 15a94983 2018-12-23 stsp err = got_object_open_by_id_str(&obj, repo, id_str);
469 15a94983 2018-12-23 stsp return err;
471 15a94983 2018-12-23 stsp *id = got_object_id_dup(got_object_get_id(obj));
472 15a94983 2018-12-23 stsp got_object_close(obj);
473 15a94983 2018-12-23 stsp if (*id == NULL)
474 15a94983 2018-12-23 stsp return got_error_from_errno();
476 15a94983 2018-12-23 stsp return NULL;
479 434025f3 2018-09-16 stsp static const struct got_error *
480 a158c901 2018-12-23 stsp request_packed_commit(struct got_commit_object **commit, struct got_pack *pack,
481 a158c901 2018-12-23 stsp int pack_idx, struct got_object_id *id)
483 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
485 a158c901 2018-12-23 stsp err = got_privsep_send_commit_req(pack->privsep_child->ibuf, -1, id,
488 a158c901 2018-12-23 stsp return err;
490 a158c901 2018-12-23 stsp return got_privsep_recv_commit(commit, pack->privsep_child->ibuf);
493 a158c901 2018-12-23 stsp static const struct got_error *
494 a158c901 2018-12-23 stsp read_packed_commit_privsep(struct got_commit_object **commit,
495 a158c901 2018-12-23 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
496 a158c901 2018-12-23 stsp struct got_object_id *id)
498 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
500 a158c901 2018-12-23 stsp if (pack->privsep_child)
501 a158c901 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
503 711fb6e8 2018-12-23 stsp err = start_pack_privsep_child(pack, packidx);
505 a158c901 2018-12-23 stsp return err;
507 711fb6e8 2018-12-23 stsp return request_packed_commit(commit, pack, idx, id);
510 9f2369b0 2018-12-24 stsp static const struct got_error *
511 9f2369b0 2018-12-24 stsp request_commit(struct got_commit_object **commit, struct got_repository *repo,
514 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
515 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
517 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf;
519 9f2369b0 2018-12-24 stsp err = got_privsep_send_commit_req(ibuf, fd, NULL, -1);
521 9f2369b0 2018-12-24 stsp return err;
523 9f2369b0 2018-12-24 stsp return got_privsep_recv_commit(commit, ibuf);
526 9f2369b0 2018-12-24 stsp static const struct got_error *
527 9f2369b0 2018-12-24 stsp read_commit_privsep(struct got_commit_object **commit, int obj_fd,
528 9f2369b0 2018-12-24 stsp struct got_repository *repo)
530 9f2369b0 2018-12-24 stsp int imsg_fds[2];
532 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
534 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd != -1)
535 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
537 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
538 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
539 9f2369b0 2018-12-24 stsp return got_error_from_errno();
541 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
542 9f2369b0 2018-12-24 stsp return got_error_from_errno();
544 9f2369b0 2018-12-24 stsp pid = fork();
545 9f2369b0 2018-12-24 stsp if (pid == -1)
546 9f2369b0 2018-12-24 stsp return got_error_from_errno();
547 9f2369b0 2018-12-24 stsp else if (pid == 0) {
548 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_COMMIT,
549 9f2369b0 2018-12-24 stsp repo->path);
550 9f2369b0 2018-12-24 stsp /* not reached */
553 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
554 3a6ce05a 2019-02-11 stsp return got_error_from_errno();
555 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].imsg_fd =
556 9f2369b0 2018-12-24 stsp imsg_fds[0];
557 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].pid = pid;
558 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
559 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_COMMIT].ibuf = ibuf;
561 9f2369b0 2018-12-24 stsp return request_commit(commit, repo, obj_fd);
565 a158c901 2018-12-23 stsp static const struct got_error *
566 434025f3 2018-09-16 stsp open_commit(struct got_commit_object **commit,
567 1785f84a 2018-12-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
569 434025f3 2018-09-16 stsp const struct got_error *err = NULL;
570 1785f84a 2018-12-23 stsp struct got_packidx *packidx = NULL;
572 1785f84a 2018-12-23 stsp char *path_packfile;
574 434025f3 2018-09-16 stsp if (check_cache) {
575 1785f84a 2018-12-23 stsp *commit = got_repo_get_cached_commit(repo, id);
576 434025f3 2018-09-16 stsp if (*commit != NULL) {
577 434025f3 2018-09-16 stsp (*commit)->refcnt++;
578 434025f3 2018-09-16 stsp return NULL;
581 434025f3 2018-09-16 stsp *commit = NULL;
583 1785f84a 2018-12-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
584 1785f84a 2018-12-23 stsp if (err == NULL) {
585 1785f84a 2018-12-23 stsp struct got_pack *pack = NULL;
587 1785f84a 2018-12-23 stsp err = get_packfile_path(&path_packfile, packidx);
589 1785f84a 2018-12-23 stsp return err;
591 1785f84a 2018-12-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
592 434025f3 2018-09-16 stsp if (pack == NULL) {
593 1785f84a 2018-12-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
596 434025f3 2018-09-16 stsp return err;
598 a158c901 2018-12-23 stsp err = read_packed_commit_privsep(commit, pack,
599 1785f84a 2018-12-23 stsp packidx, idx, id);
600 1785f84a 2018-12-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
603 1785f84a 2018-12-23 stsp err = open_loose_object(&fd, id, repo);
605 434025f3 2018-09-16 stsp return err;
606 9f2369b0 2018-12-24 stsp err = read_commit_privsep(commit, fd, repo);
609 434025f3 2018-09-16 stsp if (err == NULL) {
610 434025f3 2018-09-16 stsp (*commit)->refcnt++;
611 1785f84a 2018-12-23 stsp err = got_repo_cache_commit(repo, id, *commit);
614 e32baab7 2018-11-05 stsp return err;
617 e32baab7 2018-11-05 stsp const struct got_error *
618 e32baab7 2018-11-05 stsp got_object_open_as_commit(struct got_commit_object **commit,
619 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object_id *id)
621 e32baab7 2018-11-05 stsp *commit = got_repo_get_cached_commit(repo, id);
622 e32baab7 2018-11-05 stsp if (*commit != NULL) {
623 e32baab7 2018-11-05 stsp (*commit)->refcnt++;
624 e32baab7 2018-11-05 stsp return NULL;
627 1785f84a 2018-12-23 stsp return open_commit(commit, repo, id, 0);
630 e32baab7 2018-11-05 stsp const struct got_error *
631 e32baab7 2018-11-05 stsp got_object_commit_open(struct got_commit_object **commit,
632 e32baab7 2018-11-05 stsp struct got_repository *repo, struct got_object *obj)
634 1785f84a 2018-12-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
637 434025f3 2018-09-16 stsp const struct got_error *
638 dbc6a6b6 2018-07-12 stsp got_object_qid_alloc(struct got_object_qid **qid, struct got_object_id *id)
640 dbc6a6b6 2018-07-12 stsp const struct got_error *err = NULL;
642 dbc6a6b6 2018-07-12 stsp *qid = calloc(1, sizeof(**qid));
643 dbc6a6b6 2018-07-12 stsp if (*qid == NULL)
644 dbc6a6b6 2018-07-12 stsp return got_error_from_errno();
646 dbc6a6b6 2018-07-12 stsp (*qid)->id = got_object_id_dup(id);
647 dbc6a6b6 2018-07-12 stsp if ((*qid)->id == NULL) {
648 dbc6a6b6 2018-07-12 stsp err = got_error_from_errno();
649 fa2f6902 2018-07-23 stsp got_object_qid_free(*qid);
650 dbc6a6b6 2018-07-12 stsp *qid = NULL;
651 dbc6a6b6 2018-07-12 stsp return err;
654 dbc6a6b6 2018-07-12 stsp return NULL;
657 a158c901 2018-12-23 stsp static const struct got_error *
658 13c729f7 2018-12-24 stsp request_packed_tree(struct got_tree_object **tree, struct got_pack *pack,
659 13c729f7 2018-12-24 stsp int pack_idx, struct got_object_id *id)
661 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
663 13c729f7 2018-12-24 stsp err = got_privsep_send_tree_req(pack->privsep_child->ibuf, -1, id,
666 a158c901 2018-12-23 stsp return err;
668 a158c901 2018-12-23 stsp return got_privsep_recv_tree(tree, pack->privsep_child->ibuf);
671 71eb0e7f 2018-09-16 stsp static const struct got_error *
672 13c729f7 2018-12-24 stsp read_packed_tree_privsep(struct got_tree_object **tree,
673 13c729f7 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
674 13c729f7 2018-12-24 stsp struct got_object_id *id)
676 13c729f7 2018-12-24 stsp const struct got_error *err = NULL;
678 13c729f7 2018-12-24 stsp if (pack->privsep_child)
679 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
681 13c729f7 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
683 13c729f7 2018-12-24 stsp return err;
685 13c729f7 2018-12-24 stsp return request_packed_tree(tree, pack, idx, id);
688 13c729f7 2018-12-24 stsp static const struct got_error *
689 9f2369b0 2018-12-24 stsp request_tree(struct got_tree_object **tree, struct got_repository *repo,
692 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
693 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
695 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf;
697 9f2369b0 2018-12-24 stsp err = got_privsep_send_tree_req(ibuf, fd, NULL, -1);
699 9f2369b0 2018-12-24 stsp return err;
701 9f2369b0 2018-12-24 stsp return got_privsep_recv_tree(tree, ibuf);
704 9f2369b0 2018-12-24 stsp const struct got_error *
705 9f2369b0 2018-12-24 stsp read_tree_privsep(struct got_tree_object **tree, int obj_fd,
706 9f2369b0 2018-12-24 stsp struct got_repository *repo)
708 9f2369b0 2018-12-24 stsp int imsg_fds[2];
710 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
712 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd != -1)
713 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
715 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
716 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
717 9f2369b0 2018-12-24 stsp return got_error_from_errno();
719 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
720 9f2369b0 2018-12-24 stsp return got_error_from_errno();
722 9f2369b0 2018-12-24 stsp pid = fork();
723 9f2369b0 2018-12-24 stsp if (pid == -1)
724 9f2369b0 2018-12-24 stsp return got_error_from_errno();
725 9f2369b0 2018-12-24 stsp else if (pid == 0) {
726 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TREE,
727 9f2369b0 2018-12-24 stsp repo->path);
728 9f2369b0 2018-12-24 stsp /* not reached */
731 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
732 3a6ce05a 2019-02-11 stsp return got_error_from_errno();
733 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].imsg_fd =
734 9f2369b0 2018-12-24 stsp imsg_fds[0];
735 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].pid = pid;
736 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
737 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TREE].ibuf = ibuf;
740 9f2369b0 2018-12-24 stsp return request_tree(tree, repo, obj_fd);
743 9f2369b0 2018-12-24 stsp static const struct got_error *
744 13c729f7 2018-12-24 stsp open_tree(struct got_tree_object **tree, struct got_repository *repo,
745 13c729f7 2018-12-24 stsp struct got_object_id *id, int check_cache)
747 0ffeb3c2 2017-11-26 stsp const struct got_error *err = NULL;
748 13c729f7 2018-12-24 stsp struct got_packidx *packidx = NULL;
750 13c729f7 2018-12-24 stsp char *path_packfile;
752 71eb0e7f 2018-09-16 stsp if (check_cache) {
753 13c729f7 2018-12-24 stsp *tree = got_repo_get_cached_tree(repo, id);
754 71eb0e7f 2018-09-16 stsp if (*tree != NULL) {
755 71eb0e7f 2018-09-16 stsp (*tree)->refcnt++;
756 71eb0e7f 2018-09-16 stsp return NULL;
759 71eb0e7f 2018-09-16 stsp *tree = NULL;
761 13c729f7 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
762 13c729f7 2018-12-24 stsp if (err == NULL) {
763 13c729f7 2018-12-24 stsp struct got_pack *pack = NULL;
765 13c729f7 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
767 13c729f7 2018-12-24 stsp return err;
769 13c729f7 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
770 e7885405 2018-09-10 stsp if (pack == NULL) {
771 13c729f7 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
774 e7885405 2018-09-10 stsp return err;
776 13c729f7 2018-12-24 stsp err = read_packed_tree_privsep(tree, pack,
777 13c729f7 2018-12-24 stsp packidx, idx, id);
778 13c729f7 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
781 13c729f7 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
783 e0ab43e7 2018-03-16 stsp return err;
784 9f2369b0 2018-12-24 stsp err = read_tree_privsep(tree, fd, repo);
787 f6be5c30 2018-06-22 stsp if (err == NULL) {
788 f6be5c30 2018-06-22 stsp (*tree)->refcnt++;
789 13c729f7 2018-12-24 stsp err = got_repo_cache_tree(repo, id, *tree);
792 776d4d29 2018-06-17 stsp return err;
795 776d4d29 2018-06-17 stsp const struct got_error *
796 776d4d29 2018-06-17 stsp got_object_open_as_tree(struct got_tree_object **tree,
797 776d4d29 2018-06-17 stsp struct got_repository *repo, struct got_object_id *id)
799 e8eb494a 2018-09-16 stsp *tree = got_repo_get_cached_tree(repo, id);
800 e8eb494a 2018-09-16 stsp if (*tree != NULL) {
801 e8eb494a 2018-09-16 stsp (*tree)->refcnt++;
802 e8eb494a 2018-09-16 stsp return NULL;
805 13c729f7 2018-12-24 stsp return open_tree(tree, repo, id, 0);
808 71eb0e7f 2018-09-16 stsp const struct got_error *
809 71eb0e7f 2018-09-16 stsp got_object_tree_open(struct got_tree_object **tree,
810 71eb0e7f 2018-09-16 stsp struct got_repository *repo, struct got_object *obj)
812 13c729f7 2018-12-24 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
815 883f0469 2018-06-23 stsp const struct got_tree_entries *
816 883f0469 2018-06-23 stsp got_object_tree_get_entries(struct got_tree_object *tree)
818 883f0469 2018-06-23 stsp return &tree->entries;
821 3840f4c9 2018-09-12 stsp static const struct got_error *
822 ac544f8c 2019-01-13 stsp request_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
823 ebc55e2d 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
824 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
826 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
827 3840f4c9 2018-09-12 stsp int outfd_child;
828 3840f4c9 2018-09-12 stsp int basefd, accumfd; /* temporary files for delta application */
830 3840f4c9 2018-09-12 stsp basefd = got_opentempfd();
831 3840f4c9 2018-09-12 stsp if (basefd == -1)
832 3840f4c9 2018-09-12 stsp return got_error_from_errno();
833 3840f4c9 2018-09-12 stsp accumfd = got_opentempfd();
834 3840f4c9 2018-09-12 stsp if (accumfd == -1)
835 3840f4c9 2018-09-12 stsp return got_error_from_errno();
837 3840f4c9 2018-09-12 stsp outfd_child = dup(outfd);
838 3840f4c9 2018-09-12 stsp if (outfd_child == -1)
839 3840f4c9 2018-09-12 stsp return got_error_from_errno();
841 ebc55e2d 2018-12-24 stsp err = got_privsep_send_blob_req(pack->privsep_child->ibuf, -1, id, idx);
843 3840f4c9 2018-09-12 stsp return err;
845 3840f4c9 2018-09-12 stsp err = got_privsep_send_blob_outfd(pack->privsep_child->ibuf,
846 3840f4c9 2018-09-12 stsp outfd_child);
848 41496140 2019-02-21 stsp close(basefd);
849 41496140 2019-02-21 stsp close(accumfd);
850 3840f4c9 2018-09-12 stsp return err;
853 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
856 3840f4c9 2018-09-12 stsp close(accumfd);
857 3840f4c9 2018-09-12 stsp return err;
860 3840f4c9 2018-09-12 stsp err = got_privsep_send_tmpfd(pack->privsep_child->ibuf,
863 3840f4c9 2018-09-12 stsp return err;
865 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, size, hdrlen,
866 ebc55e2d 2018-12-24 stsp pack->privsep_child->ibuf);
868 3840f4c9 2018-09-12 stsp return err;
870 3840f4c9 2018-09-12 stsp if (lseek(outfd, SEEK_SET, 0) == -1)
871 3840f4c9 2018-09-12 stsp err = got_error_from_errno();
873 3840f4c9 2018-09-12 stsp return err;
876 ebc55e2d 2018-12-24 stsp static const struct got_error *
877 ac544f8c 2019-01-13 stsp read_packed_blob_privsep(uint8_t **outbuf, size_t *size, size_t *hdrlen,
878 ac544f8c 2019-01-13 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
879 ebc55e2d 2018-12-24 stsp struct got_object_id *id)
881 68482ea3 2017-11-27 stsp const struct got_error *err = NULL;
883 ebc55e2d 2018-12-24 stsp if (pack->privsep_child == NULL) {
884 ebc55e2d 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
886 ebc55e2d 2018-12-24 stsp return err;
889 ac544f8c 2019-01-13 stsp return request_packed_blob(outbuf, size, hdrlen, outfd, pack, packidx,
893 9f2369b0 2018-12-24 stsp static const struct got_error *
894 ac544f8c 2019-01-13 stsp request_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd,
895 ac544f8c 2019-01-13 stsp int infd, struct imsgbuf *ibuf)
897 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
898 9f2369b0 2018-12-24 stsp int outfd_child;
900 9f2369b0 2018-12-24 stsp outfd_child = dup(outfd);
901 9f2369b0 2018-12-24 stsp if (outfd_child == -1)
902 9f2369b0 2018-12-24 stsp return got_error_from_errno();
904 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_req(ibuf, infd, NULL, -1);
906 9f2369b0 2018-12-24 stsp return err;
908 9f2369b0 2018-12-24 stsp err = got_privsep_send_blob_outfd(ibuf, outfd_child);
910 9f2369b0 2018-12-24 stsp return err;
912 ac544f8c 2019-01-13 stsp err = got_privsep_recv_blob(outbuf, 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 ac544f8c 2019-01-13 stsp read_blob_privsep(uint8_t **outbuf, 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 ac544f8c 2019-01-13 stsp return request_blob(outbuf, 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 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
952 3a6ce05a 2019-02-11 stsp return got_error_from_errno();
953 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].imsg_fd =
954 9f2369b0 2018-12-24 stsp imsg_fds[0];
955 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].pid = pid;
956 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
957 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_BLOB].ibuf = ibuf;
959 ac544f8c 2019-01-13 stsp return request_blob(outbuf, size, hdrlen, outfd, infd, ibuf);
962 ebc55e2d 2018-12-24 stsp static const struct got_error *
963 ebc55e2d 2018-12-24 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
964 ebc55e2d 2018-12-24 stsp struct got_object_id *id, size_t blocksize)
966 ebc55e2d 2018-12-24 stsp const struct got_error *err = NULL;
967 ebc55e2d 2018-12-24 stsp struct got_packidx *packidx = NULL;
969 ebc55e2d 2018-12-24 stsp char *path_packfile;
970 ac544f8c 2019-01-13 stsp uint8_t *outbuf;
972 ebc55e2d 2018-12-24 stsp size_t size, hdrlen;
973 ebc55e2d 2018-12-24 stsp struct stat sb;
975 68482ea3 2017-11-27 stsp *blob = calloc(1, sizeof(**blob));
976 4558fcd4 2018-01-14 stsp if (*blob == NULL)
977 0a585a0d 2018-03-17 stsp return got_error_from_errno();
979 55da3778 2018-09-10 stsp outfd = got_opentempfd();
980 55da3778 2018-09-10 stsp if (outfd == -1)
981 55da3778 2018-09-10 stsp return got_error_from_errno();
983 062ebb78 2018-07-12 stsp (*blob)->read_buf = malloc(blocksize);
984 15c8b0e6 2018-04-24 stsp if ((*blob)->read_buf == NULL) {
985 15c8b0e6 2018-04-24 stsp err = got_error_from_errno();
989 ebc55e2d 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
990 ebc55e2d 2018-12-24 stsp if (err == NULL) {
991 ebc55e2d 2018-12-24 stsp struct got_pack *pack = NULL;
993 ebc55e2d 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
997 ebc55e2d 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
998 55da3778 2018-09-10 stsp if (pack == NULL) {
999 ebc55e2d 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1002 55da3778 2018-09-10 stsp goto done;
1004 ac544f8c 2019-01-13 stsp err = read_packed_blob_privsep(&outbuf, &size, &hdrlen, outfd,
1005 ac544f8c 2019-01-13 stsp pack, packidx, idx, id);
1006 ebc55e2d 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1009 ebc55e2d 2018-12-24 stsp err = open_loose_object(&infd, id, repo);
1011 c7254d79 2018-04-24 stsp goto done;
1012 ac544f8c 2019-01-13 stsp err = read_blob_privsep(&outbuf, &size, &hdrlen, outfd, infd,
1016 55da3778 2018-09-10 stsp goto done;
1018 de060dff 2018-12-24 stsp if (hdrlen > size) {
1019 ebc55e2d 2018-12-24 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
1020 ebc55e2d 2018-12-24 stsp goto done;
1023 ac544f8c 2019-01-13 stsp if (outbuf) {
1024 3a6ce05a 2019-02-11 stsp if (close(outfd) != 0 && err == NULL)
1025 3a6ce05a 2019-02-11 stsp err = got_error_from_errno();
1026 ac544f8c 2019-01-13 stsp outfd = -1;
1027 ac544f8c 2019-01-13 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
1028 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1029 ac544f8c 2019-01-13 stsp err = got_error_from_errno();
1030 ac544f8c 2019-01-13 stsp free(outbuf);
1031 ac544f8c 2019-01-13 stsp goto done;
1033 ac544f8c 2019-01-13 stsp (*blob)->data = outbuf;
1035 ac544f8c 2019-01-13 stsp if (fstat(outfd, &sb) == -1) {
1036 ac544f8c 2019-01-13 stsp err = got_error_from_errno();
1037 ac544f8c 2019-01-13 stsp goto done;
1040 ac544f8c 2019-01-13 stsp if (sb.st_size != size) {
1041 ac544f8c 2019-01-13 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1042 ac544f8c 2019-01-13 stsp goto done;
1045 ac544f8c 2019-01-13 stsp (*blob)->f = fdopen(outfd, "rb");
1046 ac544f8c 2019-01-13 stsp if ((*blob)->f == NULL) {
1047 ac544f8c 2019-01-13 stsp err = got_error_from_errno();
1048 ac544f8c 2019-01-13 stsp close(outfd);
1049 ac544f8c 2019-01-13 stsp outfd = -1;
1050 ac544f8c 2019-01-13 stsp goto done;
1054 ebc55e2d 2018-12-24 stsp (*blob)->hdrlen = hdrlen;
1055 eb651edf 2018-02-11 stsp (*blob)->blocksize = blocksize;
1056 ebc55e2d 2018-12-24 stsp memcpy(&(*blob)->id.sha1, id->sha1, SHA1_DIGEST_LENGTH);
1059 55da3778 2018-09-10 stsp if (err) {
1060 55da3778 2018-09-10 stsp if (*blob) {
1061 7baf5860 2019-03-19 stsp got_object_blob_close(*blob);
1062 55da3778 2018-09-10 stsp *blob = NULL;
1063 55da3778 2018-09-10 stsp } else if (outfd != -1)
1064 55da3778 2018-09-10 stsp close(outfd);
1066 a19581a2 2018-06-21 stsp return err;
1069 a19581a2 2018-06-21 stsp const struct got_error *
1070 a19581a2 2018-06-21 stsp got_object_open_as_blob(struct got_blob_object **blob,
1071 a19581a2 2018-06-21 stsp struct got_repository *repo, struct got_object_id *id,
1072 a19581a2 2018-06-21 stsp size_t blocksize)
1074 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, id, blocksize);
1077 ebc55e2d 2018-12-24 stsp const struct got_error *
1078 ebc55e2d 2018-12-24 stsp got_object_blob_open(struct got_blob_object **blob,
1079 ebc55e2d 2018-12-24 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize)
1081 ebc55e2d 2018-12-24 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize);
1084 fb43ecf1 2019-02-11 stsp const struct got_error *
1085 68482ea3 2017-11-27 stsp got_object_blob_close(struct got_blob_object *blob)
1087 fb43ecf1 2019-02-11 stsp const struct got_error *err = NULL;
1088 15c8b0e6 2018-04-24 stsp free(blob->read_buf);
1089 7baf5860 2019-03-19 stsp if (blob->f && fclose(blob->f) != 0)
1090 fb43ecf1 2019-02-11 stsp err = got_error_from_errno();
1091 ac544f8c 2019-01-13 stsp free(blob->data);
1092 68482ea3 2017-11-27 stsp free(blob);
1093 fb43ecf1 2019-02-11 stsp return err;
1097 f934cf2c 2018-02-12 stsp got_object_blob_id_str(struct got_blob_object *blob, char *buf, size_t size)
1099 f934cf2c 2018-02-12 stsp return got_sha1_digest_to_str(blob->id.sha1, buf, size);
1103 f934cf2c 2018-02-12 stsp got_object_blob_get_hdrlen(struct got_blob_object *blob)
1105 f934cf2c 2018-02-12 stsp return blob->hdrlen;
1108 f934cf2c 2018-02-12 stsp const uint8_t *
1109 f934cf2c 2018-02-12 stsp got_object_blob_get_read_buf(struct got_blob_object *blob)
1111 f934cf2c 2018-02-12 stsp return blob->read_buf;
1114 68482ea3 2017-11-27 stsp const struct got_error *
1115 eb651edf 2018-02-11 stsp got_object_blob_read_block(size_t *outlenp, struct got_blob_object *blob)
1119 eb651edf 2018-02-11 stsp n = fread(blob->read_buf, 1, blob->blocksize, blob->f);
1120 eb651edf 2018-02-11 stsp if (n == 0 && ferror(blob->f))
1121 eb651edf 2018-02-11 stsp return got_ferror(blob->f, GOT_ERR_IO);
1122 eb651edf 2018-02-11 stsp *outlenp = n;
1123 35e9ba5d 2018-06-21 stsp return NULL;
1126 35e9ba5d 2018-06-21 stsp const struct got_error *
1127 6fcac457 2018-11-19 stsp got_object_blob_dump_to_file(size_t *total_len, int *nlines,
1128 84451b3e 2018-07-10 stsp FILE *outfile, struct got_blob_object *blob)
1130 35e9ba5d 2018-06-21 stsp const struct got_error *err = NULL;
1131 b6752625 2018-12-24 stsp size_t n, len, hdrlen;
1132 84451b3e 2018-07-10 stsp const uint8_t *buf;
1135 84451b3e 2018-07-10 stsp if (total_len)
1136 84451b3e 2018-07-10 stsp *total_len = 0;
1137 84451b3e 2018-07-10 stsp if (nlines)
1138 84451b3e 2018-07-10 stsp *nlines = 0;
1140 35e9ba5d 2018-06-21 stsp hdrlen = got_object_blob_get_hdrlen(blob);
1142 35e9ba5d 2018-06-21 stsp err = got_object_blob_read_block(&len, blob);
1144 35e9ba5d 2018-06-21 stsp return err;
1145 35e9ba5d 2018-06-21 stsp if (len == 0)
1147 84451b3e 2018-07-10 stsp if (total_len)
1148 84451b3e 2018-07-10 stsp *total_len += len;
1149 84451b3e 2018-07-10 stsp buf = got_object_blob_get_read_buf(blob);
1150 84451b3e 2018-07-10 stsp if (nlines) {
1151 84451b3e 2018-07-10 stsp for (i = 0; i < len; i++) {
1152 84451b3e 2018-07-10 stsp if (buf[i] == '\n')
1153 84451b3e 2018-07-10 stsp (*nlines)++;
1156 35e9ba5d 2018-06-21 stsp /* Skip blob object header first time around. */
1157 454a6b59 2018-12-24 stsp n = fwrite(buf + hdrlen, 1, len - hdrlen, outfile);
1158 b6752625 2018-12-24 stsp if (n != len - hdrlen)
1159 b6752625 2018-12-24 stsp return got_ferror(outfile, GOT_ERR_IO);
1160 35e9ba5d 2018-06-21 stsp hdrlen = 0;
1161 35e9ba5d 2018-06-21 stsp } while (len != 0);
1163 cbe7f848 2019-02-11 stsp if (fflush(outfile) != 0)
1164 cbe7f848 2019-02-11 stsp return got_error_from_errno();
1165 35e9ba5d 2018-06-21 stsp rewind(outfile);
1167 776d4d29 2018-06-17 stsp return NULL;
1170 f4a881ce 2018-11-17 stsp static const struct got_error *
1171 268f7291 2018-12-24 stsp request_packed_tag(struct got_tag_object **tag, struct got_pack *pack,
1172 268f7291 2018-12-24 stsp int pack_idx, struct got_object_id *id)
1174 a158c901 2018-12-23 stsp const struct got_error *err = NULL;
1176 268f7291 2018-12-24 stsp err = got_privsep_send_tag_req(pack->privsep_child->ibuf, -1, id,
1177 268f7291 2018-12-24 stsp pack_idx);
1179 a158c901 2018-12-23 stsp return err;
1181 a158c901 2018-12-23 stsp return got_privsep_recv_tag(tag, pack->privsep_child->ibuf);
1184 268f7291 2018-12-24 stsp static const struct got_error *
1185 268f7291 2018-12-24 stsp read_packed_tag_privsep(struct got_tag_object **tag,
1186 268f7291 2018-12-24 stsp struct got_pack *pack, struct got_packidx *packidx, int idx,
1187 268f7291 2018-12-24 stsp struct got_object_id *id)
1189 268f7291 2018-12-24 stsp const struct got_error *err = NULL;
1191 268f7291 2018-12-24 stsp if (pack->privsep_child)
1192 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1194 268f7291 2018-12-24 stsp err = start_pack_privsep_child(pack, packidx);
1196 268f7291 2018-12-24 stsp return err;
1198 268f7291 2018-12-24 stsp return request_packed_tag(tag, pack, idx, id);
1201 9f2369b0 2018-12-24 stsp static const struct got_error *
1202 9f2369b0 2018-12-24 stsp request_tag(struct got_tag_object **tag, struct got_repository *repo,
1205 9f2369b0 2018-12-24 stsp const struct got_error *err = NULL;
1206 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1208 9f2369b0 2018-12-24 stsp ibuf = repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf;
1210 9f2369b0 2018-12-24 stsp err = got_privsep_send_tag_req(ibuf, fd, NULL, -1);
1212 9f2369b0 2018-12-24 stsp return err;
1214 9f2369b0 2018-12-24 stsp return got_privsep_recv_tag(tag, ibuf);
1217 9f2369b0 2018-12-24 stsp static const struct got_error *
1218 9f2369b0 2018-12-24 stsp read_tag_privsep(struct got_tag_object **tag, int obj_fd,
1219 9f2369b0 2018-12-24 stsp struct got_repository *repo)
1221 9f2369b0 2018-12-24 stsp int imsg_fds[2];
1222 9f2369b0 2018-12-24 stsp pid_t pid;
1223 9f2369b0 2018-12-24 stsp struct imsgbuf *ibuf;
1225 9f2369b0 2018-12-24 stsp if (repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd != -1)
1226 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1228 9f2369b0 2018-12-24 stsp ibuf = calloc(1, sizeof(*ibuf));
1229 9f2369b0 2018-12-24 stsp if (ibuf == NULL)
1230 9f2369b0 2018-12-24 stsp return got_error_from_errno();
1232 9f2369b0 2018-12-24 stsp if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, imsg_fds) == -1)
1233 9f2369b0 2018-12-24 stsp return got_error_from_errno();
1235 9f2369b0 2018-12-24 stsp pid = fork();
1236 9f2369b0 2018-12-24 stsp if (pid == -1)
1237 9f2369b0 2018-12-24 stsp return got_error_from_errno();
1238 9f2369b0 2018-12-24 stsp else if (pid == 0) {
1239 9f2369b0 2018-12-24 stsp exec_privsep_child(imsg_fds, GOT_PATH_PROG_READ_TAG,
1240 9f2369b0 2018-12-24 stsp repo->path);
1241 9f2369b0 2018-12-24 stsp /* not reached */
1244 3a6ce05a 2019-02-11 stsp if (close(imsg_fds[1]) != 0)
1245 3a6ce05a 2019-02-11 stsp return got_error_from_errno();
1246 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].imsg_fd =
1247 9f2369b0 2018-12-24 stsp imsg_fds[0];
1248 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].pid = pid;
1249 9f2369b0 2018-12-24 stsp imsg_init(ibuf, imsg_fds[0]);
1250 9f2369b0 2018-12-24 stsp repo->privsep_children[GOT_REPO_PRIVSEP_CHILD_TAG].ibuf = ibuf;
1252 9f2369b0 2018-12-24 stsp return request_tag(tag, repo, obj_fd);
1255 a158c901 2018-12-23 stsp static const struct got_error *
1256 268f7291 2018-12-24 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
1257 268f7291 2018-12-24 stsp struct got_object_id *id, int check_cache)
1259 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
1260 268f7291 2018-12-24 stsp struct got_packidx *packidx = NULL;
1262 268f7291 2018-12-24 stsp char *path_packfile;
1264 f4a881ce 2018-11-17 stsp if (check_cache) {
1265 268f7291 2018-12-24 stsp *tag = got_repo_get_cached_tag(repo, id);
1266 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1267 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1268 f4a881ce 2018-11-17 stsp return NULL;
1271 f4a881ce 2018-11-17 stsp *tag = NULL;
1273 268f7291 2018-12-24 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
1274 268f7291 2018-12-24 stsp if (err == NULL) {
1275 268f7291 2018-12-24 stsp struct got_pack *pack = NULL;
1277 268f7291 2018-12-24 stsp err = get_packfile_path(&path_packfile, packidx);
1279 268f7291 2018-12-24 stsp return err;
1281 268f7291 2018-12-24 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
1282 f4a881ce 2018-11-17 stsp if (pack == NULL) {
1283 268f7291 2018-12-24 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
1286 f4a881ce 2018-11-17 stsp return err;
1288 268f7291 2018-12-24 stsp err = read_packed_tag_privsep(tag, pack,
1289 268f7291 2018-12-24 stsp packidx, idx, id);
1290 268f7291 2018-12-24 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
1293 268f7291 2018-12-24 stsp err = open_loose_object(&fd, id, repo);
1295 f4a881ce 2018-11-17 stsp return err;
1296 9f2369b0 2018-12-24 stsp err = read_tag_privsep(tag, fd, repo);
1299 f4a881ce 2018-11-17 stsp if (err == NULL) {
1300 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1301 268f7291 2018-12-24 stsp err = got_repo_cache_tag(repo, id, *tag);
1304 f4a881ce 2018-11-17 stsp return err;
1307 f4a881ce 2018-11-17 stsp const struct got_error *
1308 f4a881ce 2018-11-17 stsp got_object_open_as_tag(struct got_tag_object **tag,
1309 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object_id *id)
1311 f4a881ce 2018-11-17 stsp *tag = got_repo_get_cached_tag(repo, id);
1312 f4a881ce 2018-11-17 stsp if (*tag != NULL) {
1313 f4a881ce 2018-11-17 stsp (*tag)->refcnt++;
1314 f4a881ce 2018-11-17 stsp return NULL;
1317 268f7291 2018-12-24 stsp return open_tag(tag, repo, id, 0);
1320 f4a881ce 2018-11-17 stsp const struct got_error *
1321 f4a881ce 2018-11-17 stsp got_object_tag_open(struct got_tag_object **tag,
1322 f4a881ce 2018-11-17 stsp struct got_repository *repo, struct got_object *obj)
1324 268f7291 2018-12-24 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
1328 0bd18d37 2019-02-01 stsp got_object_tag_get_object_type(struct got_tag_object *tag)
1330 0bd18d37 2019-02-01 stsp return tag->obj_type;
1333 0bd18d37 2019-02-01 stsp struct got_object_id *
1334 0bd18d37 2019-02-01 stsp got_object_tag_get_object_id(struct got_tag_object *tag)
1336 0bd18d37 2019-02-01 stsp return &tag->id;
1339 776d4d29 2018-06-17 stsp static struct got_tree_entry *
1340 65a9bbe9 2018-09-15 stsp find_entry_by_name(struct got_tree_object *tree, const char *name, size_t len)
1342 776d4d29 2018-06-17 stsp struct got_tree_entry *te;
1344 63da309a 2018-11-07 stsp /* Note that tree entries are sorted in strncmp() order. */
1345 883f0469 2018-06-23 stsp SIMPLEQ_FOREACH(te, &tree->entries.head, entry) {
1346 63da309a 2018-11-07 stsp int cmp = strncmp(te->name, name, len);
1347 63da309a 2018-11-07 stsp if (cmp < 0)
1349 63da309a 2018-11-07 stsp if (cmp > 0)
1351 63da309a 2018-11-07 stsp if (te->name[len] == '\0')
1352 776d4d29 2018-06-17 stsp return te;
1354 eb651edf 2018-02-11 stsp return NULL;
1357 a129376b 2019-03-28 stsp const struct got_tree_entry *
1358 a129376b 2019-03-28 stsp got_object_tree_find_entry(struct got_tree_object *tree, const char *name)
1360 a129376b 2019-03-28 stsp return find_entry_by_name(tree, name, strlen(name));
1363 776d4d29 2018-06-17 stsp const struct got_error *
1364 27d434c2 2018-09-15 stsp got_object_id_by_path(struct got_object_id **id, struct got_repository *repo,
1365 776d4d29 2018-06-17 stsp struct got_object_id *commit_id, const char *path)
1367 776d4d29 2018-06-17 stsp const struct got_error *err = NULL;
1368 776d4d29 2018-06-17 stsp struct got_commit_object *commit = NULL;
1369 776d4d29 2018-06-17 stsp struct got_tree_object *tree = NULL;
1370 db37e2c0 2018-06-21 stsp struct got_tree_entry *te = NULL;
1371 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1372 b7cd37e5 2018-11-18 stsp size_t seglen;
1374 27d434c2 2018-09-15 stsp *id = NULL;
1376 776d4d29 2018-06-17 stsp /* We are expecting an absolute in-repository path. */
1377 776d4d29 2018-06-17 stsp if (path[0] != '/')
1378 776d4d29 2018-06-17 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1380 776d4d29 2018-06-17 stsp err = got_object_open_as_commit(&commit, repo, commit_id);
1382 776d4d29 2018-06-17 stsp goto done;
1384 776d4d29 2018-06-17 stsp /* Handle opening of root of commit's tree. */
1385 776d4d29 2018-06-17 stsp if (path[1] == '\0') {
1386 27d434c2 2018-09-15 stsp *id = got_object_id_dup(commit->tree_id);
1387 27d434c2 2018-09-15 stsp if (*id == NULL)
1388 27d434c2 2018-09-15 stsp err = got_error_from_errno();
1389 ca008b32 2018-07-22 stsp goto done;
1392 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&tree, repo, commit->tree_id);
1394 776d4d29 2018-06-17 stsp goto done;
1397 776d4d29 2018-06-17 stsp s++; /* skip leading '/' */
1399 65a9bbe9 2018-09-15 stsp seglen = 0;
1400 b7cd37e5 2018-11-18 stsp while (*s) {
1401 776d4d29 2018-06-17 stsp struct got_tree_object *next_tree;
1403 776d4d29 2018-06-17 stsp if (*s != '/') {
1410 65a9bbe9 2018-09-15 stsp te = find_entry_by_name(tree, seg, seglen);
1411 db37e2c0 2018-06-21 stsp if (te == NULL) {
1412 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1413 776d4d29 2018-06-17 stsp goto done;
1416 b7cd37e5 2018-11-18 stsp if (*s == '\0')
1419 776d4d29 2018-06-17 stsp seg = s + 1;
1420 65a9bbe9 2018-09-15 stsp seglen = 0;
1423 776d4d29 2018-06-17 stsp err = got_object_open_as_tree(&next_tree, repo,
1425 db37e2c0 2018-06-21 stsp te = NULL;
1427 776d4d29 2018-06-17 stsp goto done;
1428 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1429 776d4d29 2018-06-17 stsp tree = next_tree;
1434 27d434c2 2018-09-15 stsp *id = got_object_id_dup(te->id);
1435 27d434c2 2018-09-15 stsp if (*id == NULL)
1436 27d434c2 2018-09-15 stsp return got_error_from_errno();
1438 d1451975 2018-11-11 stsp err = got_error(GOT_ERR_NO_TREE_ENTRY);
1440 776d4d29 2018-06-17 stsp if (commit)
1441 776d4d29 2018-06-17 stsp got_object_commit_close(commit);
1443 776d4d29 2018-06-17 stsp got_object_tree_close(tree);
1444 776d4d29 2018-06-17 stsp return err;
1447 07862c20 2018-09-15 stsp const struct got_error *
1448 07862c20 2018-09-15 stsp got_object_tree_path_changed(int *changed,
1449 07862c20 2018-09-15 stsp struct got_tree_object *tree01, struct got_tree_object *tree02,
1450 07862c20 2018-09-15 stsp const char *path, struct got_repository *repo)
1452 07862c20 2018-09-15 stsp const struct got_error *err = NULL;
1453 07862c20 2018-09-15 stsp struct got_tree_object *tree1 = NULL, *tree2 = NULL;
1454 07862c20 2018-09-15 stsp struct got_tree_entry *te1 = NULL, *te2 = NULL;
1455 65a9bbe9 2018-09-15 stsp const char *seg, *s;
1456 3b7f9878 2018-11-18 stsp size_t seglen;
1458 07862c20 2018-09-15 stsp *changed = 0;
1460 07862c20 2018-09-15 stsp /* We are expecting an absolute in-repository path. */
1461 07862c20 2018-09-15 stsp if (path[0] != '/')
1462 07862c20 2018-09-15 stsp return got_error(GOT_ERR_NOT_ABSPATH);
1464 07862c20 2018-09-15 stsp /* We not do support comparing the root path. */
1465 07862c20 2018-09-15 stsp if (path[1] == '\0')
1466 07862c20 2018-09-15 stsp return got_error(GOT_ERR_BAD_PATH);
1468 07862c20 2018-09-15 stsp tree1 = tree01;
1469 07862c20 2018-09-15 stsp tree2 = tree02;
1471 07862c20 2018-09-15 stsp s++; /* skip leading '/' */
1473 65a9bbe9 2018-09-15 stsp seglen = 0;
1474 3b7f9878 2018-11-18 stsp while (*s) {
1475 07862c20 2018-09-15 stsp struct got_tree_object *next_tree1, *next_tree2;
1477 07862c20 2018-09-15 stsp if (*s != '/') {
1484 65a9bbe9 2018-09-15 stsp te1 = find_entry_by_name(tree1, seg, seglen);
1485 07862c20 2018-09-15 stsp if (te1 == NULL) {
1486 07862c20 2018-09-15 stsp err = got_error(GOT_ERR_NO_OBJ);
1487 07862c20 2018-09-15 stsp goto done;
1490 65a9bbe9 2018-09-15 stsp te2 = find_entry_by_name(tree2, seg, seglen);
1491 07862c20 2018-09-15 stsp if (te2 == NULL) {
1492 07862c20 2018-09-15 stsp *changed = 1;
1493 07862c20 2018-09-15 stsp goto done;
1496 07862c20 2018-09-15 stsp if (te1->mode != te2->mode) {
1497 07862c20 2018-09-15 stsp *changed = 1;
1498 07862c20 2018-09-15 stsp goto done;
1501 07862c20 2018-09-15 stsp if (got_object_id_cmp(te1->id, te2->id) == 0) {
1502 07862c20 2018-09-15 stsp *changed = 0;
1503 07862c20 2018-09-15 stsp goto done;
1506 3b7f9878 2018-11-18 stsp if (*s == '\0') { /* final path element */
1507 07862c20 2018-09-15 stsp *changed = 1;
1508 07862c20 2018-09-15 stsp goto done;
1511 07862c20 2018-09-15 stsp seg = s + 1;
1513 65a9bbe9 2018-09-15 stsp seglen = 0;
1515 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree1, repo,
1517 07862c20 2018-09-15 stsp te1 = NULL;
1519 07862c20 2018-09-15 stsp goto done;
1520 a31cea73 2018-09-15 stsp if (tree1 != tree01)
1521 a31cea73 2018-09-15 stsp got_object_tree_close(tree1);
1522 07862c20 2018-09-15 stsp tree1 = next_tree1;
1524 07862c20 2018-09-15 stsp err = got_object_open_as_tree(&next_tree2, repo,
1526 07862c20 2018-09-15 stsp te2 = NULL;
1528 07862c20 2018-09-15 stsp goto done;
1529 a31cea73 2018-09-15 stsp if (tree2 != tree02)
1530 a31cea73 2018-09-15 stsp got_object_tree_close(tree2);
1531 07862c20 2018-09-15 stsp tree2 = next_tree2;
1535 a31cea73 2018-09-15 stsp if (tree1 && tree1 != tree01)
1536 07862c20 2018-09-15 stsp got_object_tree_close(tree1);
1537 a31cea73 2018-09-15 stsp if (tree2 && tree2 != tree02)
1538 07862c20 2018-09-15 stsp got_object_tree_close(tree2);
1539 77880158 2018-11-04 stsp return err;
1542 ed175427 2019-05-09 stsp const struct got_error *
1543 ed175427 2019-05-09 stsp got_object_tree_entry_dup(struct got_tree_entry **new_te,
1544 ed175427 2019-05-09 stsp struct got_tree_entry *te)
1546 ed175427 2019-05-09 stsp const struct got_error *err = NULL;
1548 ed175427 2019-05-09 stsp *new_te = calloc(1, sizeof(**new_te));
1549 ed175427 2019-05-09 stsp if (*new_te == NULL)
1550 ed175427 2019-05-09 stsp return got_error_from_errno();
1552 ed175427 2019-05-09 stsp (*new_te)->mode = te->mode;
1553 ed175427 2019-05-09 stsp (*new_te)->name = strdup(te->name);
1554 ed175427 2019-05-09 stsp if ((*new_te)->name == NULL) {
1555 ed175427 2019-05-09 stsp err = got_error_from_errno();
1556 ed175427 2019-05-09 stsp got_object_tree_entry_close(*new_te);
1557 ed175427 2019-05-09 stsp return err;
1560 ed175427 2019-05-09 stsp (*new_te)->id = got_object_id_dup(te->id);
1561 ed175427 2019-05-09 stsp if ((*new_te)->id == NULL) {
1562 ed175427 2019-05-09 stsp err = got_error_from_errno();
1563 ed175427 2019-05-09 stsp got_object_tree_entry_close(*new_te);
1564 ed175427 2019-05-09 stsp return err;
1567 ed175427 2019-05-09 stsp return NULL;