2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 13b2bc37 2022-10-23 stsp * Permission to use, copy, modify, and distribute this software for any
5 13b2bc37 2022-10-23 stsp * purpose with or without fee is hereby granted, provided that the above
6 13b2bc37 2022-10-23 stsp * copyright notice and this permission notice appear in all copies.
8 13b2bc37 2022-10-23 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 13b2bc37 2022-10-23 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 13b2bc37 2022-10-23 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 13b2bc37 2022-10-23 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 13b2bc37 2022-10-23 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 13b2bc37 2022-10-23 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 13b2bc37 2022-10-23 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 13b2bc37 2022-10-23 stsp #include <sys/queue.h>
18 13b2bc37 2022-10-23 stsp #include <sys/tree.h>
19 13b2bc37 2022-10-23 stsp #include <sys/stat.h>
21 13b2bc37 2022-10-23 stsp #include <errno.h>
22 13b2bc37 2022-10-23 stsp #include <limits.h>
23 13b2bc37 2022-10-23 stsp #include <sha1.h>
24 13b2bc37 2022-10-23 stsp #include <stdio.h>
25 13b2bc37 2022-10-23 stsp #include <stdlib.h>
26 13b2bc37 2022-10-23 stsp #include <string.h>
27 13b2bc37 2022-10-23 stsp #include <unistd.h>
29 13b2bc37 2022-10-23 stsp #include "got_error.h"
30 13b2bc37 2022-10-23 stsp #include "got_object.h"
31 13b2bc37 2022-10-23 stsp #include "got_repository.h"
32 13b2bc37 2022-10-23 stsp #include "got_path.h"
34 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
35 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
36 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
37 13b2bc37 2022-10-23 stsp #include "got_lib_object_parse.h"
38 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
39 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
41 13b2bc37 2022-10-23 stsp const struct got_error *
42 13b2bc37 2022-10-23 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
43 13b2bc37 2022-10-23 stsp struct got_repository *repo)
45 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
46 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
47 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
49 13b2bc37 2022-10-23 stsp char *path_packfile;
51 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
55 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
56 13b2bc37 2022-10-23 stsp packidx->path_packidx);
60 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
61 13b2bc37 2022-10-23 stsp if (pack == NULL) {
62 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
67 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
70 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
72 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
74 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
75 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
79 13b2bc37 2022-10-23 stsp free(path_packfile);
83 13b2bc37 2022-10-23 stsp const struct got_error *
84 13b2bc37 2022-10-23 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
85 13b2bc37 2022-10-23 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
86 13b2bc37 2022-10-23 stsp struct got_repository *repo)
88 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
91 13b2bc37 2022-10-23 stsp const struct got_error *
92 13b2bc37 2022-10-23 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
93 13b2bc37 2022-10-23 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
94 13b2bc37 2022-10-23 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
95 13b2bc37 2022-10-23 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
96 13b2bc37 2022-10-23 stsp struct got_repository *repo)
98 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
101 13b2bc37 2022-10-23 stsp const struct got_error *
102 13b2bc37 2022-10-23 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
103 13b2bc37 2022-10-23 stsp struct got_object_id *id)
105 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
108 13b2bc37 2022-10-23 stsp *obj = got_repo_get_cached_object(repo, id);
109 13b2bc37 2022-10-23 stsp if (*obj != NULL) {
110 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
111 13b2bc37 2022-10-23 stsp return NULL;
114 13b2bc37 2022-10-23 stsp err = got_object_open_packed(obj, id, repo);
116 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NO_OBJ)
117 13b2bc37 2022-10-23 stsp return err;
119 13b2bc37 2022-10-23 stsp return NULL;
121 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
123 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
124 13b2bc37 2022-10-23 stsp err = got_error_no_obj(id);
125 13b2bc37 2022-10-23 stsp return err;
128 13b2bc37 2022-10-23 stsp err = got_object_read_header(obj, fd);
132 13b2bc37 2022-10-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
133 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
135 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
137 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
138 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
139 13b2bc37 2022-10-23 stsp err = NULL;
142 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
143 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
144 13b2bc37 2022-10-23 stsp return err;
147 13b2bc37 2022-10-23 stsp static const struct got_error *
148 13b2bc37 2022-10-23 stsp wrap_fd(FILE **f, int wrapped_fd)
150 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
153 13b2bc37 2022-10-23 stsp if (ftruncate(wrapped_fd, 0L) == -1)
154 13b2bc37 2022-10-23 stsp return got_error_from_errno("ftruncate");
156 13b2bc37 2022-10-23 stsp if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
157 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
159 13b2bc37 2022-10-23 stsp fd = dup(wrapped_fd);
160 13b2bc37 2022-10-23 stsp if (fd == -1)
161 13b2bc37 2022-10-23 stsp return got_error_from_errno("dup");
163 13b2bc37 2022-10-23 stsp *f = fdopen(fd, "w+");
164 13b2bc37 2022-10-23 stsp if (*f == NULL) {
165 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
168 13b2bc37 2022-10-23 stsp return err;
171 13b2bc37 2022-10-23 stsp static const struct got_error *
172 13b2bc37 2022-10-23 stsp read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
173 13b2bc37 2022-10-23 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
174 13b2bc37 2022-10-23 stsp struct got_object_id *id)
176 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
177 13b2bc37 2022-10-23 stsp uint64_t raw_size = 0;
178 13b2bc37 2022-10-23 stsp struct got_object *obj;
179 13b2bc37 2022-10-23 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
181 13b2bc37 2022-10-23 stsp *outbuf = NULL;
183 13b2bc37 2022-10-23 stsp *hdrlen = 0;
185 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
187 13b2bc37 2022-10-23 stsp return err;
189 13b2bc37 2022-10-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
190 13b2bc37 2022-10-23 stsp err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
194 13b2bc37 2022-10-23 stsp raw_size = obj->size;
196 13b2bc37 2022-10-23 stsp if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
197 13b2bc37 2022-10-23 stsp size_t len;
198 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(outbuf, &len,
199 13b2bc37 2022-10-23 stsp obj, pack);
202 13b2bc37 2022-10-23 stsp *size = (off_t)len;
205 13b2bc37 2022-10-23 stsp * XXX This uses 3 file extra descriptors for no good reason.
206 13b2bc37 2022-10-23 stsp * We should have got_packfile_extract_object_to_fd().
208 13b2bc37 2022-10-23 stsp err = wrap_fd(&outfile, outfd);
211 13b2bc37 2022-10-23 stsp err = wrap_fd(&basefile, pack->basefd);
214 13b2bc37 2022-10-23 stsp err = wrap_fd(&accumfile, pack->accumfd);
217 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
218 13b2bc37 2022-10-23 stsp accumfile);
221 95bdb85d 2023-01-09 stsp *size = obj->size;
224 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
226 13b2bc37 2022-10-23 stsp got_object_close(obj);
227 13b2bc37 2022-10-23 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
228 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
229 13b2bc37 2022-10-23 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
230 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
231 13b2bc37 2022-10-23 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
232 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
233 13b2bc37 2022-10-23 stsp return err;
237 13b2bc37 2022-10-23 stsp static void
238 13b2bc37 2022-10-23 stsp put_raw_object_tempfile(struct got_raw_object *obj)
240 13b2bc37 2022-10-23 stsp struct got_repository *repo = obj->close_arg;
242 13b2bc37 2022-10-23 stsp if (obj->tempfile_idx != -1)
243 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(obj->tempfile_idx, repo);
246 13b2bc37 2022-10-23 stsp /* *outfd must be initialized to -1 by caller */
247 13b2bc37 2022-10-23 stsp const struct got_error *
248 13b2bc37 2022-10-23 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
249 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
251 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
252 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
253 3a975a9f 2023-01-09 stsp int idx, tempfd, tempfile_idx;
254 13b2bc37 2022-10-23 stsp uint8_t *outbuf = NULL;
255 13b2bc37 2022-10-23 stsp off_t size = 0;
256 13b2bc37 2022-10-23 stsp size_t hdrlen = 0;
257 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
259 13b2bc37 2022-10-23 stsp *obj = got_repo_get_cached_raw_object(repo, id);
260 13b2bc37 2022-10-23 stsp if (*obj != NULL) {
261 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
262 13b2bc37 2022-10-23 stsp return NULL;
265 3a975a9f 2023-01-09 stsp err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
267 3a975a9f 2023-01-09 stsp return err;
269 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
270 13b2bc37 2022-10-23 stsp if (err == NULL) {
271 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
273 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
274 13b2bc37 2022-10-23 stsp packidx->path_packidx);
278 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
279 13b2bc37 2022-10-23 stsp if (pack == NULL) {
280 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
285 13b2bc37 2022-10-23 stsp err = read_packed_object_raw(&outbuf, &size, &hdrlen,
286 3a975a9f 2023-01-09 stsp tempfd, pack, packidx, idx, id);
289 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
292 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
295 13b2bc37 2022-10-23 stsp err = got_object_read_raw(&outbuf, &size, &hdrlen,
296 3a975a9f 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
297 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
298 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
303 3a975a9f 2023-01-09 stsp if (outbuf == NULL) {
304 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
305 3a975a9f 2023-01-09 stsp err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
310 3a975a9f 2023-01-09 stsp * Duplicate tempfile descriptor to allow use of
311 3a975a9f 2023-01-09 stsp * fdopen(3) inside got_object_raw_alloc().
313 3a975a9f 2023-01-09 stsp *outfd = dup(tempfd);
314 3a975a9f 2023-01-09 stsp if (*outfd == -1) {
315 3a975a9f 2023-01-09 stsp err = got_error_from_errno("dup");
320 60c140ae 2023-01-09 stsp err = got_object_raw_alloc(obj, outbuf, outfd,
321 60c140ae 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
325 13b2bc37 2022-10-23 stsp err = got_repo_cache_raw_object(repo, id, *obj);
327 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
328 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
329 13b2bc37 2022-10-23 stsp err = NULL;
332 13b2bc37 2022-10-23 stsp free(path_packfile);
334 13b2bc37 2022-10-23 stsp if (*obj) {
335 13b2bc37 2022-10-23 stsp got_object_raw_close(*obj);
336 13b2bc37 2022-10-23 stsp *obj = NULL;
338 13b2bc37 2022-10-23 stsp free(outbuf);
339 3a975a9f 2023-01-09 stsp got_repo_temp_fds_put(tempfile_idx, repo);
340 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
341 3a975a9f 2023-01-09 stsp close(*outfd);
342 3a975a9f 2023-01-09 stsp *outfd = -1;
345 3a975a9f 2023-01-09 stsp if (((*obj)->f == NULL && (*obj)->fd == -1)) {
346 3a975a9f 2023-01-09 stsp /* This raw object is not backed by a file. */
347 3a975a9f 2023-01-09 stsp got_repo_temp_fds_put(tempfile_idx, repo);
348 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
349 3a975a9f 2023-01-09 stsp close(*outfd);
350 3a975a9f 2023-01-09 stsp *outfd = -1;
353 3a975a9f 2023-01-09 stsp (*obj)->tempfile_idx = tempfile_idx;
354 3a975a9f 2023-01-09 stsp (*obj)->close_cb = put_raw_object_tempfile;
355 3a975a9f 2023-01-09 stsp (*obj)->close_arg = repo;
358 13b2bc37 2022-10-23 stsp return err;
361 13b2bc37 2022-10-23 stsp static const struct got_error *
362 13b2bc37 2022-10-23 stsp open_commit(struct got_commit_object **commit,
363 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
365 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
366 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
368 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
370 13b2bc37 2022-10-23 stsp if (check_cache) {
371 13b2bc37 2022-10-23 stsp *commit = got_repo_get_cached_commit(repo, id);
372 13b2bc37 2022-10-23 stsp if (*commit != NULL) {
373 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
374 13b2bc37 2022-10-23 stsp return NULL;
377 13b2bc37 2022-10-23 stsp *commit = NULL;
379 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
380 13b2bc37 2022-10-23 stsp if (err == NULL) {
381 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
382 13b2bc37 2022-10-23 stsp struct got_object *obj;
383 13b2bc37 2022-10-23 stsp uint8_t *buf;
384 13b2bc37 2022-10-23 stsp size_t len;
386 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
387 13b2bc37 2022-10-23 stsp packidx->path_packidx);
389 13b2bc37 2022-10-23 stsp return err;
391 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
392 13b2bc37 2022-10-23 stsp if (pack == NULL) {
393 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
398 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
401 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
402 13b2bc37 2022-10-23 stsp obj, pack);
403 13b2bc37 2022-10-23 stsp got_object_close(obj);
406 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, buf, len);
408 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
411 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
413 13b2bc37 2022-10-23 stsp return err;
414 13b2bc37 2022-10-23 stsp err = got_object_read_commit(commit, fd, id, 0);
415 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
416 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
418 13b2bc37 2022-10-23 stsp return err;
421 13b2bc37 2022-10-23 stsp if (err == NULL) {
422 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
423 13b2bc37 2022-10-23 stsp err = got_repo_cache_commit(repo, id, *commit);
425 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
426 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
427 13b2bc37 2022-10-23 stsp err = NULL;
431 13b2bc37 2022-10-23 stsp free(path_packfile);
432 13b2bc37 2022-10-23 stsp return err;
435 13b2bc37 2022-10-23 stsp const struct got_error *
436 13b2bc37 2022-10-23 stsp got_object_open_as_commit(struct got_commit_object **commit,
437 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
439 13b2bc37 2022-10-23 stsp *commit = got_repo_get_cached_commit(repo, id);
440 13b2bc37 2022-10-23 stsp if (*commit != NULL) {
441 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
442 13b2bc37 2022-10-23 stsp return NULL;
445 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, id, 0);
448 13b2bc37 2022-10-23 stsp const struct got_error *
449 13b2bc37 2022-10-23 stsp got_object_commit_open(struct got_commit_object **commit,
450 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
452 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
455 13b2bc37 2022-10-23 stsp static const struct got_error *
456 13b2bc37 2022-10-23 stsp open_tree(struct got_tree_object **tree,
457 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
459 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
460 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
462 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
463 13b2bc37 2022-10-23 stsp struct got_parsed_tree_entry *entries = NULL;
464 13b2bc37 2022-10-23 stsp size_t nentries = 0, nentries_alloc = 0, i;
465 13b2bc37 2022-10-23 stsp uint8_t *buf = NULL;
467 13b2bc37 2022-10-23 stsp if (check_cache) {
468 13b2bc37 2022-10-23 stsp *tree = got_repo_get_cached_tree(repo, id);
469 13b2bc37 2022-10-23 stsp if (*tree != NULL) {
470 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
471 13b2bc37 2022-10-23 stsp return NULL;
474 13b2bc37 2022-10-23 stsp *tree = NULL;
476 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
477 13b2bc37 2022-10-23 stsp if (err == NULL) {
478 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
479 13b2bc37 2022-10-23 stsp struct got_object *obj;
480 13b2bc37 2022-10-23 stsp size_t len;
482 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
483 13b2bc37 2022-10-23 stsp packidx->path_packidx);
485 13b2bc37 2022-10-23 stsp return err;
487 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
488 13b2bc37 2022-10-23 stsp if (pack == NULL) {
489 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
494 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
497 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
498 13b2bc37 2022-10-23 stsp obj, pack);
499 13b2bc37 2022-10-23 stsp got_object_close(obj);
502 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(&entries, &nentries,
503 13b2bc37 2022-10-23 stsp &nentries_alloc, buf, len);
506 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
509 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
511 13b2bc37 2022-10-23 stsp return err;
512 13b2bc37 2022-10-23 stsp err = got_object_read_tree(&entries, &nentries,
513 13b2bc37 2022-10-23 stsp &nentries_alloc, &buf, fd, id);
514 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
515 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
521 13b2bc37 2022-10-23 stsp *tree = malloc(sizeof(**tree));
522 13b2bc37 2022-10-23 stsp if (*tree == NULL) {
523 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
526 13b2bc37 2022-10-23 stsp (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
527 13b2bc37 2022-10-23 stsp if ((*tree)->entries == NULL) {
528 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
531 13b2bc37 2022-10-23 stsp (*tree)->nentries = nentries;
532 13b2bc37 2022-10-23 stsp (*tree)->refcnt = 0;
534 13b2bc37 2022-10-23 stsp for (i = 0; i < nentries; i++) {
535 13b2bc37 2022-10-23 stsp struct got_parsed_tree_entry *pe = &entries[i];
536 13b2bc37 2022-10-23 stsp struct got_tree_entry *te = &(*tree)->entries[i];
538 13b2bc37 2022-10-23 stsp if (strlcpy(te->name, pe->name,
539 13b2bc37 2022-10-23 stsp sizeof(te->name)) >= sizeof(te->name)) {
540 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
543 13b2bc37 2022-10-23 stsp memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
544 13b2bc37 2022-10-23 stsp te->mode = pe->mode;
545 13b2bc37 2022-10-23 stsp te->idx = i;
548 13b2bc37 2022-10-23 stsp free(path_packfile);
549 13b2bc37 2022-10-23 stsp free(entries);
551 13b2bc37 2022-10-23 stsp if (err == NULL) {
552 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
553 13b2bc37 2022-10-23 stsp err = got_repo_cache_tree(repo, id, *tree);
555 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
556 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
557 13b2bc37 2022-10-23 stsp err = NULL;
562 13b2bc37 2022-10-23 stsp free((*tree)->entries);
563 13b2bc37 2022-10-23 stsp free(*tree);
564 13b2bc37 2022-10-23 stsp *tree = NULL;
566 13b2bc37 2022-10-23 stsp return err;
569 13b2bc37 2022-10-23 stsp const struct got_error *
570 13b2bc37 2022-10-23 stsp got_object_open_as_tree(struct got_tree_object **tree,
571 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
573 13b2bc37 2022-10-23 stsp *tree = got_repo_get_cached_tree(repo, id);
574 13b2bc37 2022-10-23 stsp if (*tree != NULL) {
575 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
576 13b2bc37 2022-10-23 stsp return NULL;
579 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, id, 0);
582 13b2bc37 2022-10-23 stsp const struct got_error *
583 13b2bc37 2022-10-23 stsp got_object_tree_open(struct got_tree_object **tree,
584 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
586 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
589 13b2bc37 2022-10-23 stsp const struct got_error *
590 13b2bc37 2022-10-23 stsp got_object_open_as_blob(struct got_blob_object **blob,
591 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, size_t blocksize,
594 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
597 13b2bc37 2022-10-23 stsp const struct got_error *
598 13b2bc37 2022-10-23 stsp got_object_blob_open(struct got_blob_object **blob,
599 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize,
602 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
605 13b2bc37 2022-10-23 stsp static const struct got_error *
606 13b2bc37 2022-10-23 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
607 13b2bc37 2022-10-23 stsp struct got_object_id *id, int check_cache)
609 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
610 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
612 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
613 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
614 13b2bc37 2022-10-23 stsp int obj_type = GOT_OBJ_TYPE_ANY;
616 13b2bc37 2022-10-23 stsp if (check_cache) {
617 13b2bc37 2022-10-23 stsp *tag = got_repo_get_cached_tag(repo, id);
618 13b2bc37 2022-10-23 stsp if (*tag != NULL) {
619 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
620 13b2bc37 2022-10-23 stsp return NULL;
623 13b2bc37 2022-10-23 stsp *tag = NULL;
625 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
626 13b2bc37 2022-10-23 stsp if (err == NULL) {
627 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
628 13b2bc37 2022-10-23 stsp uint8_t *buf = NULL;
629 13b2bc37 2022-10-23 stsp size_t len;
631 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
632 13b2bc37 2022-10-23 stsp packidx->path_packidx);
634 13b2bc37 2022-10-23 stsp return err;
636 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
637 13b2bc37 2022-10-23 stsp if (pack == NULL) {
638 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
644 13b2bc37 2022-10-23 stsp /* Beware of "lightweight" tags: Check object type first. */
645 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
648 13b2bc37 2022-10-23 stsp obj_type = obj->type;
649 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
650 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
651 13b2bc37 2022-10-23 stsp got_object_close(obj);
654 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
655 13b2bc37 2022-10-23 stsp obj, pack);
656 13b2bc37 2022-10-23 stsp got_object_close(obj);
659 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, buf, len);
661 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
664 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
666 13b2bc37 2022-10-23 stsp return err;
667 13b2bc37 2022-10-23 stsp err = got_object_read_header(&obj, fd);
668 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
669 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
671 13b2bc37 2022-10-23 stsp return err;
672 13b2bc37 2022-10-23 stsp obj_type = obj->type;
673 13b2bc37 2022-10-23 stsp got_object_close(obj);
674 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
675 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
677 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
679 13b2bc37 2022-10-23 stsp return err;
680 13b2bc37 2022-10-23 stsp err = got_object_read_tag(tag, fd, id, 0);
681 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
682 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
684 13b2bc37 2022-10-23 stsp return err;
687 13b2bc37 2022-10-23 stsp if (err == NULL) {
688 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
689 13b2bc37 2022-10-23 stsp err = got_repo_cache_tag(repo, id, *tag);
691 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
692 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
693 13b2bc37 2022-10-23 stsp err = NULL;
697 13b2bc37 2022-10-23 stsp free(path_packfile);
698 13b2bc37 2022-10-23 stsp return err;
701 13b2bc37 2022-10-23 stsp const struct got_error *
702 13b2bc37 2022-10-23 stsp got_object_open_as_tag(struct got_tag_object **tag,
703 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
705 13b2bc37 2022-10-23 stsp *tag = got_repo_get_cached_tag(repo, id);
706 13b2bc37 2022-10-23 stsp if (*tag != NULL) {
707 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
708 13b2bc37 2022-10-23 stsp return NULL;
711 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, id, 0);
714 13b2bc37 2022-10-23 stsp const struct got_error *
715 13b2bc37 2022-10-23 stsp got_object_tag_open(struct got_tag_object **tag,
716 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
718 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);