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 5822e79e 2023-02-23 op #include <sha2.h>
25 13b2bc37 2022-10-23 stsp #include <stdio.h>
26 13b2bc37 2022-10-23 stsp #include <stdlib.h>
27 13b2bc37 2022-10-23 stsp #include <string.h>
28 13b2bc37 2022-10-23 stsp #include <unistd.h>
30 13b2bc37 2022-10-23 stsp #include "got_error.h"
31 13b2bc37 2022-10-23 stsp #include "got_object.h"
32 13b2bc37 2022-10-23 stsp #include "got_repository.h"
33 13b2bc37 2022-10-23 stsp #include "got_path.h"
35 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
36 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
37 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
38 13b2bc37 2022-10-23 stsp #include "got_lib_object_parse.h"
39 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
40 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
42 13b2bc37 2022-10-23 stsp const struct got_error *
43 13b2bc37 2022-10-23 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
44 13b2bc37 2022-10-23 stsp struct got_repository *repo)
46 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
47 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
48 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
50 13b2bc37 2022-10-23 stsp char *path_packfile;
52 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
56 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
57 13b2bc37 2022-10-23 stsp packidx->path_packidx);
61 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
62 13b2bc37 2022-10-23 stsp if (pack == NULL) {
63 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
68 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
71 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
73 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
75 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
76 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
80 13b2bc37 2022-10-23 stsp free(path_packfile);
84 13b2bc37 2022-10-23 stsp const struct got_error *
85 13b2bc37 2022-10-23 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
86 13b2bc37 2022-10-23 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
87 13b2bc37 2022-10-23 stsp struct got_repository *repo)
89 9afa3de2 2023-04-04 stsp const struct got_error *err;
91 9afa3de2 2023-04-04 stsp *obj = got_repo_get_cached_object(repo, id);
92 9afa3de2 2023-04-04 stsp if (*obj != NULL) {
93 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
94 9afa3de2 2023-04-04 stsp return NULL;
97 9afa3de2 2023-04-04 stsp err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
100 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
102 9afa3de2 2023-04-04 stsp err = got_repo_cache_object(repo, id, *obj);
104 9afa3de2 2023-04-04 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
105 9afa3de2 2023-04-04 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
106 9afa3de2 2023-04-04 stsp err = NULL;
107 9afa3de2 2023-04-04 stsp return err;
109 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
110 9afa3de2 2023-04-04 stsp return NULL;
113 13b2bc37 2022-10-23 stsp const struct got_error *
114 13b2bc37 2022-10-23 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
115 13b2bc37 2022-10-23 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
116 13b2bc37 2022-10-23 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
117 13b2bc37 2022-10-23 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
118 13b2bc37 2022-10-23 stsp struct got_repository *repo)
120 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
123 13b2bc37 2022-10-23 stsp const struct got_error *
124 13b2bc37 2022-10-23 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
125 13b2bc37 2022-10-23 stsp struct got_object_id *id)
127 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
130 13b2bc37 2022-10-23 stsp *obj = got_repo_get_cached_object(repo, id);
131 13b2bc37 2022-10-23 stsp if (*obj != NULL) {
132 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
133 13b2bc37 2022-10-23 stsp return NULL;
136 13b2bc37 2022-10-23 stsp err = got_object_open_packed(obj, id, repo);
138 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NO_OBJ)
139 13b2bc37 2022-10-23 stsp return err;
141 13b2bc37 2022-10-23 stsp return NULL;
143 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
145 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
146 13b2bc37 2022-10-23 stsp err = got_error_no_obj(id);
147 13b2bc37 2022-10-23 stsp return err;
150 13b2bc37 2022-10-23 stsp err = got_object_read_header(obj, fd);
154 13b2bc37 2022-10-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
155 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
157 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
159 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
160 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
161 13b2bc37 2022-10-23 stsp err = NULL;
164 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
165 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
166 13b2bc37 2022-10-23 stsp return err;
169 13b2bc37 2022-10-23 stsp static const struct got_error *
170 13b2bc37 2022-10-23 stsp wrap_fd(FILE **f, int wrapped_fd)
172 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
175 13b2bc37 2022-10-23 stsp if (ftruncate(wrapped_fd, 0L) == -1)
176 13b2bc37 2022-10-23 stsp return got_error_from_errno("ftruncate");
178 13b2bc37 2022-10-23 stsp if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
179 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
181 13b2bc37 2022-10-23 stsp fd = dup(wrapped_fd);
182 13b2bc37 2022-10-23 stsp if (fd == -1)
183 13b2bc37 2022-10-23 stsp return got_error_from_errno("dup");
185 13b2bc37 2022-10-23 stsp *f = fdopen(fd, "w+");
186 13b2bc37 2022-10-23 stsp if (*f == NULL) {
187 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
190 13b2bc37 2022-10-23 stsp return err;
193 13b2bc37 2022-10-23 stsp static const struct got_error *
194 13b2bc37 2022-10-23 stsp read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
195 13b2bc37 2022-10-23 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
196 13b2bc37 2022-10-23 stsp struct got_object_id *id)
198 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
199 13b2bc37 2022-10-23 stsp uint64_t raw_size = 0;
200 13b2bc37 2022-10-23 stsp struct got_object *obj;
201 13b2bc37 2022-10-23 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
203 13b2bc37 2022-10-23 stsp *outbuf = NULL;
205 13b2bc37 2022-10-23 stsp *hdrlen = 0;
207 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
209 13b2bc37 2022-10-23 stsp return err;
211 13b2bc37 2022-10-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
212 13b2bc37 2022-10-23 stsp err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
216 13b2bc37 2022-10-23 stsp raw_size = obj->size;
218 13b2bc37 2022-10-23 stsp if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
219 13b2bc37 2022-10-23 stsp size_t len;
220 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(outbuf, &len,
221 13b2bc37 2022-10-23 stsp obj, pack);
224 13b2bc37 2022-10-23 stsp *size = (off_t)len;
227 13b2bc37 2022-10-23 stsp * XXX This uses 3 file extra descriptors for no good reason.
228 13b2bc37 2022-10-23 stsp * We should have got_packfile_extract_object_to_fd().
230 13b2bc37 2022-10-23 stsp err = wrap_fd(&outfile, outfd);
233 13b2bc37 2022-10-23 stsp err = wrap_fd(&basefile, pack->basefd);
236 13b2bc37 2022-10-23 stsp err = wrap_fd(&accumfile, pack->accumfd);
239 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
240 13b2bc37 2022-10-23 stsp accumfile);
243 95bdb85d 2023-01-09 stsp *size = obj->size;
246 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
248 13b2bc37 2022-10-23 stsp got_object_close(obj);
249 13b2bc37 2022-10-23 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
250 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
251 13b2bc37 2022-10-23 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
252 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
253 13b2bc37 2022-10-23 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
254 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
255 13b2bc37 2022-10-23 stsp return err;
259 13b2bc37 2022-10-23 stsp static void
260 13b2bc37 2022-10-23 stsp put_raw_object_tempfile(struct got_raw_object *obj)
262 13b2bc37 2022-10-23 stsp struct got_repository *repo = obj->close_arg;
264 13b2bc37 2022-10-23 stsp if (obj->tempfile_idx != -1)
265 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(obj->tempfile_idx, repo);
268 13b2bc37 2022-10-23 stsp /* *outfd must be initialized to -1 by caller */
269 13b2bc37 2022-10-23 stsp const struct got_error *
270 13b2bc37 2022-10-23 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
271 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
273 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
274 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
275 3a975a9f 2023-01-09 stsp int idx, tempfd, tempfile_idx;
276 13b2bc37 2022-10-23 stsp uint8_t *outbuf = NULL;
277 13b2bc37 2022-10-23 stsp off_t size = 0;
278 13b2bc37 2022-10-23 stsp size_t hdrlen = 0;
279 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
281 13b2bc37 2022-10-23 stsp *obj = got_repo_get_cached_raw_object(repo, id);
282 13b2bc37 2022-10-23 stsp if (*obj != NULL) {
283 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
284 13b2bc37 2022-10-23 stsp return NULL;
287 3a975a9f 2023-01-09 stsp err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
289 3a975a9f 2023-01-09 stsp return err;
291 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
292 13b2bc37 2022-10-23 stsp if (err == NULL) {
293 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
295 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
296 13b2bc37 2022-10-23 stsp packidx->path_packidx);
300 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
301 13b2bc37 2022-10-23 stsp if (pack == NULL) {
302 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
307 13b2bc37 2022-10-23 stsp err = read_packed_object_raw(&outbuf, &size, &hdrlen,
308 3a975a9f 2023-01-09 stsp tempfd, pack, packidx, idx, id);
311 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
314 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
317 13b2bc37 2022-10-23 stsp err = got_object_read_raw(&outbuf, &size, &hdrlen,
318 3a975a9f 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
319 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
320 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
325 3a975a9f 2023-01-09 stsp if (outbuf == NULL) {
326 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
327 3a975a9f 2023-01-09 stsp err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
332 3a975a9f 2023-01-09 stsp * Duplicate tempfile descriptor to allow use of
333 3a975a9f 2023-01-09 stsp * fdopen(3) inside got_object_raw_alloc().
335 3a975a9f 2023-01-09 stsp *outfd = dup(tempfd);
336 3a975a9f 2023-01-09 stsp if (*outfd == -1) {
337 3a975a9f 2023-01-09 stsp err = got_error_from_errno("dup");
342 60c140ae 2023-01-09 stsp err = got_object_raw_alloc(obj, outbuf, outfd,
343 60c140ae 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
347 13b2bc37 2022-10-23 stsp err = got_repo_cache_raw_object(repo, id, *obj);
349 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
350 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
351 13b2bc37 2022-10-23 stsp err = NULL;
354 13b2bc37 2022-10-23 stsp free(path_packfile);
356 13b2bc37 2022-10-23 stsp if (*obj) {
357 13b2bc37 2022-10-23 stsp got_object_raw_close(*obj);
358 13b2bc37 2022-10-23 stsp *obj = NULL;
360 13b2bc37 2022-10-23 stsp free(outbuf);
361 3a975a9f 2023-01-09 stsp got_repo_temp_fds_put(tempfile_idx, repo);
362 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
363 3a975a9f 2023-01-09 stsp close(*outfd);
364 3a975a9f 2023-01-09 stsp *outfd = -1;
367 3a975a9f 2023-01-09 stsp if (((*obj)->f == NULL && (*obj)->fd == -1)) {
368 3a975a9f 2023-01-09 stsp /* This raw object is not backed by a file. */
369 3a975a9f 2023-01-09 stsp got_repo_temp_fds_put(tempfile_idx, repo);
370 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
371 3a975a9f 2023-01-09 stsp close(*outfd);
372 3a975a9f 2023-01-09 stsp *outfd = -1;
375 3a975a9f 2023-01-09 stsp (*obj)->tempfile_idx = tempfile_idx;
376 3a975a9f 2023-01-09 stsp (*obj)->close_cb = put_raw_object_tempfile;
377 3a975a9f 2023-01-09 stsp (*obj)->close_arg = repo;
380 13b2bc37 2022-10-23 stsp return err;
383 13b2bc37 2022-10-23 stsp static const struct got_error *
384 13b2bc37 2022-10-23 stsp open_commit(struct got_commit_object **commit,
385 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
387 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
388 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
390 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
392 13b2bc37 2022-10-23 stsp if (check_cache) {
393 13b2bc37 2022-10-23 stsp *commit = got_repo_get_cached_commit(repo, id);
394 13b2bc37 2022-10-23 stsp if (*commit != NULL) {
395 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
396 13b2bc37 2022-10-23 stsp return NULL;
399 13b2bc37 2022-10-23 stsp *commit = NULL;
401 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
402 13b2bc37 2022-10-23 stsp if (err == NULL) {
403 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
404 13b2bc37 2022-10-23 stsp struct got_object *obj;
405 13b2bc37 2022-10-23 stsp uint8_t *buf;
406 13b2bc37 2022-10-23 stsp size_t len;
408 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
409 13b2bc37 2022-10-23 stsp packidx->path_packidx);
411 13b2bc37 2022-10-23 stsp return err;
413 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
414 13b2bc37 2022-10-23 stsp if (pack == NULL) {
415 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
420 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
423 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
424 13b2bc37 2022-10-23 stsp obj, pack);
425 13b2bc37 2022-10-23 stsp got_object_close(obj);
428 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, buf, len);
430 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
433 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
435 13b2bc37 2022-10-23 stsp return err;
436 13b2bc37 2022-10-23 stsp err = got_object_read_commit(commit, fd, id, 0);
437 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
438 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
440 13b2bc37 2022-10-23 stsp return err;
443 13b2bc37 2022-10-23 stsp if (err == NULL) {
444 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
445 13b2bc37 2022-10-23 stsp err = got_repo_cache_commit(repo, id, *commit);
447 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
448 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
449 13b2bc37 2022-10-23 stsp err = NULL;
453 13b2bc37 2022-10-23 stsp free(path_packfile);
454 13b2bc37 2022-10-23 stsp return err;
457 13b2bc37 2022-10-23 stsp const struct got_error *
458 13b2bc37 2022-10-23 stsp got_object_open_as_commit(struct got_commit_object **commit,
459 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
461 13b2bc37 2022-10-23 stsp *commit = got_repo_get_cached_commit(repo, id);
462 13b2bc37 2022-10-23 stsp if (*commit != NULL) {
463 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
464 13b2bc37 2022-10-23 stsp return NULL;
467 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, id, 0);
470 13b2bc37 2022-10-23 stsp const struct got_error *
471 13b2bc37 2022-10-23 stsp got_object_commit_open(struct got_commit_object **commit,
472 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
474 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
477 13b2bc37 2022-10-23 stsp static const struct got_error *
478 13b2bc37 2022-10-23 stsp open_tree(struct got_tree_object **tree,
479 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
481 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
482 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
484 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
485 13b2bc37 2022-10-23 stsp struct got_parsed_tree_entry *entries = NULL;
486 13b2bc37 2022-10-23 stsp size_t nentries = 0, nentries_alloc = 0, i;
487 13b2bc37 2022-10-23 stsp uint8_t *buf = NULL;
489 13b2bc37 2022-10-23 stsp if (check_cache) {
490 13b2bc37 2022-10-23 stsp *tree = got_repo_get_cached_tree(repo, id);
491 13b2bc37 2022-10-23 stsp if (*tree != NULL) {
492 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
493 13b2bc37 2022-10-23 stsp return NULL;
496 13b2bc37 2022-10-23 stsp *tree = NULL;
498 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
499 13b2bc37 2022-10-23 stsp if (err == NULL) {
500 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
501 13b2bc37 2022-10-23 stsp struct got_object *obj;
502 13b2bc37 2022-10-23 stsp size_t len;
504 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
505 13b2bc37 2022-10-23 stsp packidx->path_packidx);
507 13b2bc37 2022-10-23 stsp return err;
509 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
510 13b2bc37 2022-10-23 stsp if (pack == NULL) {
511 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
516 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
519 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
520 13b2bc37 2022-10-23 stsp obj, pack);
521 13b2bc37 2022-10-23 stsp got_object_close(obj);
524 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(&entries, &nentries,
525 13b2bc37 2022-10-23 stsp &nentries_alloc, buf, len);
528 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
531 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
533 13b2bc37 2022-10-23 stsp return err;
534 13b2bc37 2022-10-23 stsp err = got_object_read_tree(&entries, &nentries,
535 13b2bc37 2022-10-23 stsp &nentries_alloc, &buf, fd, id);
536 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
537 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
543 13b2bc37 2022-10-23 stsp *tree = malloc(sizeof(**tree));
544 13b2bc37 2022-10-23 stsp if (*tree == NULL) {
545 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
548 13b2bc37 2022-10-23 stsp (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
549 13b2bc37 2022-10-23 stsp if ((*tree)->entries == NULL) {
550 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
553 13b2bc37 2022-10-23 stsp (*tree)->nentries = nentries;
554 13b2bc37 2022-10-23 stsp (*tree)->refcnt = 0;
556 13b2bc37 2022-10-23 stsp for (i = 0; i < nentries; i++) {
557 13b2bc37 2022-10-23 stsp struct got_parsed_tree_entry *pe = &entries[i];
558 13b2bc37 2022-10-23 stsp struct got_tree_entry *te = &(*tree)->entries[i];
560 13b2bc37 2022-10-23 stsp if (strlcpy(te->name, pe->name,
561 13b2bc37 2022-10-23 stsp sizeof(te->name)) >= sizeof(te->name)) {
562 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
565 13b2bc37 2022-10-23 stsp memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
566 13b2bc37 2022-10-23 stsp te->mode = pe->mode;
567 13b2bc37 2022-10-23 stsp te->idx = i;
570 13b2bc37 2022-10-23 stsp free(path_packfile);
571 13b2bc37 2022-10-23 stsp free(entries);
573 13b2bc37 2022-10-23 stsp if (err == NULL) {
574 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
575 13b2bc37 2022-10-23 stsp err = got_repo_cache_tree(repo, id, *tree);
577 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
578 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
579 13b2bc37 2022-10-23 stsp err = NULL;
584 13b2bc37 2022-10-23 stsp free((*tree)->entries);
585 13b2bc37 2022-10-23 stsp free(*tree);
586 13b2bc37 2022-10-23 stsp *tree = NULL;
588 13b2bc37 2022-10-23 stsp return err;
591 13b2bc37 2022-10-23 stsp const struct got_error *
592 13b2bc37 2022-10-23 stsp got_object_open_as_tree(struct got_tree_object **tree,
593 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
595 13b2bc37 2022-10-23 stsp *tree = got_repo_get_cached_tree(repo, id);
596 13b2bc37 2022-10-23 stsp if (*tree != NULL) {
597 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
598 13b2bc37 2022-10-23 stsp return NULL;
601 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, id, 0);
604 13b2bc37 2022-10-23 stsp const struct got_error *
605 13b2bc37 2022-10-23 stsp got_object_tree_open(struct got_tree_object **tree,
606 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
608 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
611 13b2bc37 2022-10-23 stsp const struct got_error *
612 13b2bc37 2022-10-23 stsp got_object_open_as_blob(struct got_blob_object **blob,
613 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, size_t blocksize,
616 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
619 13b2bc37 2022-10-23 stsp const struct got_error *
620 13b2bc37 2022-10-23 stsp got_object_blob_open(struct got_blob_object **blob,
621 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize,
624 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
627 13b2bc37 2022-10-23 stsp static const struct got_error *
628 13b2bc37 2022-10-23 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
629 13b2bc37 2022-10-23 stsp struct got_object_id *id, int check_cache)
631 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
632 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
634 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
635 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
636 13b2bc37 2022-10-23 stsp int obj_type = GOT_OBJ_TYPE_ANY;
638 13b2bc37 2022-10-23 stsp if (check_cache) {
639 13b2bc37 2022-10-23 stsp *tag = got_repo_get_cached_tag(repo, id);
640 13b2bc37 2022-10-23 stsp if (*tag != NULL) {
641 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
642 13b2bc37 2022-10-23 stsp return NULL;
645 13b2bc37 2022-10-23 stsp *tag = NULL;
647 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
648 13b2bc37 2022-10-23 stsp if (err == NULL) {
649 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
650 13b2bc37 2022-10-23 stsp uint8_t *buf = NULL;
651 13b2bc37 2022-10-23 stsp size_t len;
653 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
654 13b2bc37 2022-10-23 stsp packidx->path_packidx);
656 13b2bc37 2022-10-23 stsp return err;
658 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
659 13b2bc37 2022-10-23 stsp if (pack == NULL) {
660 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
666 13b2bc37 2022-10-23 stsp /* Beware of "lightweight" tags: Check object type first. */
667 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
670 13b2bc37 2022-10-23 stsp obj_type = obj->type;
671 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
672 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
673 13b2bc37 2022-10-23 stsp got_object_close(obj);
676 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
677 13b2bc37 2022-10-23 stsp obj, pack);
678 13b2bc37 2022-10-23 stsp got_object_close(obj);
681 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, buf, len);
683 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
686 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
688 13b2bc37 2022-10-23 stsp return err;
689 13b2bc37 2022-10-23 stsp err = got_object_read_header(&obj, fd);
690 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
691 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
693 13b2bc37 2022-10-23 stsp return err;
694 13b2bc37 2022-10-23 stsp obj_type = obj->type;
695 13b2bc37 2022-10-23 stsp got_object_close(obj);
696 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
697 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
699 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
701 13b2bc37 2022-10-23 stsp return err;
702 13b2bc37 2022-10-23 stsp err = got_object_read_tag(tag, fd, id, 0);
703 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
704 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
706 13b2bc37 2022-10-23 stsp return err;
709 13b2bc37 2022-10-23 stsp if (err == NULL) {
710 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
711 13b2bc37 2022-10-23 stsp err = got_repo_cache_tag(repo, id, *tag);
713 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
714 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
715 13b2bc37 2022-10-23 stsp err = NULL;
719 13b2bc37 2022-10-23 stsp free(path_packfile);
720 13b2bc37 2022-10-23 stsp return err;
723 13b2bc37 2022-10-23 stsp const struct got_error *
724 13b2bc37 2022-10-23 stsp got_object_open_as_tag(struct got_tag_object **tag,
725 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
727 13b2bc37 2022-10-23 stsp *tag = got_repo_get_cached_tag(repo, id);
728 13b2bc37 2022-10-23 stsp if (*tag != NULL) {
729 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
730 13b2bc37 2022-10-23 stsp return NULL;
733 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, id, 0);
736 13b2bc37 2022-10-23 stsp const struct got_error *
737 13b2bc37 2022-10-23 stsp got_object_tag_open(struct got_tag_object **tag,
738 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
740 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);