2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
4 3efd8e31 2022-10-23 thomas * Permission to use, copy, modify, and distribute this software for any
5 3efd8e31 2022-10-23 thomas * purpose with or without fee is hereby granted, provided that the above
6 3efd8e31 2022-10-23 thomas * copyright notice and this permission notice appear in all copies.
8 3efd8e31 2022-10-23 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3efd8e31 2022-10-23 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3efd8e31 2022-10-23 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3efd8e31 2022-10-23 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3efd8e31 2022-10-23 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3efd8e31 2022-10-23 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3efd8e31 2022-10-23 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 4efc8dcb 2023-08-29 thomas #include "got_compat.h"
19 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
20 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
22 3efd8e31 2022-10-23 thomas #include <errno.h>
23 3efd8e31 2022-10-23 thomas #include <limits.h>
24 3efd8e31 2022-10-23 thomas #include <stdio.h>
25 3efd8e31 2022-10-23 thomas #include <stdlib.h>
26 3efd8e31 2022-10-23 thomas #include <string.h>
27 3efd8e31 2022-10-23 thomas #include <unistd.h>
29 3efd8e31 2022-10-23 thomas #include "got_error.h"
30 3efd8e31 2022-10-23 thomas #include "got_object.h"
31 3efd8e31 2022-10-23 thomas #include "got_repository.h"
32 3efd8e31 2022-10-23 thomas #include "got_path.h"
34 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
35 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
36 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
37 3efd8e31 2022-10-23 thomas #include "got_lib_object_parse.h"
38 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
39 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
41 3efd8e31 2022-10-23 thomas const struct got_error *
42 3efd8e31 2022-10-23 thomas got_object_open_packed(struct got_object **obj, struct got_object_id *id,
43 3efd8e31 2022-10-23 thomas struct got_repository *repo)
45 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
46 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
47 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
49 3efd8e31 2022-10-23 thomas char *path_packfile;
51 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
53 3efd8e31 2022-10-23 thomas return err;
55 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
56 3efd8e31 2022-10-23 thomas packidx->path_packidx);
58 3efd8e31 2022-10-23 thomas return err;
60 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
61 3efd8e31 2022-10-23 thomas if (pack == NULL) {
62 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
64 3efd8e31 2022-10-23 thomas goto done;
67 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(obj, pack, packidx, idx, id);
69 3efd8e31 2022-10-23 thomas return err;
70 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
72 3efd8e31 2022-10-23 thomas err = got_repo_cache_object(repo, id, *obj);
73 3efd8e31 2022-10-23 thomas if (err) {
74 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
75 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
76 3efd8e31 2022-10-23 thomas err = NULL;
79 3efd8e31 2022-10-23 thomas free(path_packfile);
80 3efd8e31 2022-10-23 thomas return err;
83 3efd8e31 2022-10-23 thomas const struct got_error *
84 3efd8e31 2022-10-23 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
85 3efd8e31 2022-10-23 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
86 3efd8e31 2022-10-23 thomas struct got_repository *repo)
88 6d7eb4f7 2023-04-04 thomas const struct got_error *err;
90 6d7eb4f7 2023-04-04 thomas *obj = got_repo_get_cached_object(repo, id);
91 6d7eb4f7 2023-04-04 thomas if (*obj != NULL) {
92 6d7eb4f7 2023-04-04 thomas (*obj)->refcnt++;
93 6d7eb4f7 2023-04-04 thomas return NULL;
96 6d7eb4f7 2023-04-04 thomas err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
98 6d7eb4f7 2023-04-04 thomas return err;
99 6d7eb4f7 2023-04-04 thomas (*obj)->refcnt++;
101 6d7eb4f7 2023-04-04 thomas err = got_repo_cache_object(repo, id, *obj);
102 6d7eb4f7 2023-04-04 thomas if (err) {
103 6d7eb4f7 2023-04-04 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
104 6d7eb4f7 2023-04-04 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
105 6d7eb4f7 2023-04-04 thomas err = NULL;
106 6d7eb4f7 2023-04-04 thomas return err;
108 6d7eb4f7 2023-04-04 thomas (*obj)->refcnt++;
109 6d7eb4f7 2023-04-04 thomas return NULL;
112 3efd8e31 2022-10-23 thomas const struct got_error *
113 3efd8e31 2022-10-23 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
114 3efd8e31 2022-10-23 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
115 3efd8e31 2022-10-23 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
116 3efd8e31 2022-10-23 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
117 3efd8e31 2022-10-23 thomas struct got_repository *repo)
119 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
122 3efd8e31 2022-10-23 thomas const struct got_error *
123 3efd8e31 2022-10-23 thomas got_object_open(struct got_object **obj, struct got_repository *repo,
124 3efd8e31 2022-10-23 thomas struct got_object_id *id)
126 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
129 3efd8e31 2022-10-23 thomas *obj = got_repo_get_cached_object(repo, id);
130 3efd8e31 2022-10-23 thomas if (*obj != NULL) {
131 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
132 3efd8e31 2022-10-23 thomas return NULL;
135 3efd8e31 2022-10-23 thomas err = got_object_open_packed(obj, id, repo);
136 3efd8e31 2022-10-23 thomas if (err) {
137 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NO_OBJ)
138 3efd8e31 2022-10-23 thomas return err;
140 3efd8e31 2022-10-23 thomas return NULL;
142 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
143 3efd8e31 2022-10-23 thomas if (err) {
144 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
145 3efd8e31 2022-10-23 thomas err = got_error_no_obj(id);
146 3efd8e31 2022-10-23 thomas return err;
149 3efd8e31 2022-10-23 thomas err = got_object_read_header(obj, fd);
151 3efd8e31 2022-10-23 thomas goto done;
153 3efd8e31 2022-10-23 thomas memcpy(&(*obj)->id, id, sizeof((*obj)->id));
154 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
156 3efd8e31 2022-10-23 thomas err = got_repo_cache_object(repo, id, *obj);
157 3efd8e31 2022-10-23 thomas if (err) {
158 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
159 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
160 3efd8e31 2022-10-23 thomas err = NULL;
163 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
164 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
165 3efd8e31 2022-10-23 thomas return err;
168 3efd8e31 2022-10-23 thomas static const struct got_error *
169 3efd8e31 2022-10-23 thomas wrap_fd(FILE **f, int wrapped_fd)
171 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
174 3efd8e31 2022-10-23 thomas if (ftruncate(wrapped_fd, 0L) == -1)
175 3efd8e31 2022-10-23 thomas return got_error_from_errno("ftruncate");
177 3efd8e31 2022-10-23 thomas if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
178 3efd8e31 2022-10-23 thomas return got_error_from_errno("lseek");
180 3efd8e31 2022-10-23 thomas fd = dup(wrapped_fd);
181 3efd8e31 2022-10-23 thomas if (fd == -1)
182 3efd8e31 2022-10-23 thomas return got_error_from_errno("dup");
184 3efd8e31 2022-10-23 thomas *f = fdopen(fd, "w+");
185 3efd8e31 2022-10-23 thomas if (*f == NULL) {
186 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fdopen");
187 3efd8e31 2022-10-23 thomas close(fd);
189 3efd8e31 2022-10-23 thomas return err;
192 3efd8e31 2022-10-23 thomas static const struct got_error *
193 3efd8e31 2022-10-23 thomas read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
194 3efd8e31 2022-10-23 thomas int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
195 3efd8e31 2022-10-23 thomas struct got_object_id *id)
197 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
198 3efd8e31 2022-10-23 thomas uint64_t raw_size = 0;
199 3efd8e31 2022-10-23 thomas struct got_object *obj;
200 3efd8e31 2022-10-23 thomas FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
202 3efd8e31 2022-10-23 thomas *outbuf = NULL;
203 3efd8e31 2022-10-23 thomas *size = 0;
204 3efd8e31 2022-10-23 thomas *hdrlen = 0;
206 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
208 3efd8e31 2022-10-23 thomas return err;
210 3efd8e31 2022-10-23 thomas if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
211 3efd8e31 2022-10-23 thomas err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
213 3efd8e31 2022-10-23 thomas goto done;
215 3efd8e31 2022-10-23 thomas raw_size = obj->size;
217 3efd8e31 2022-10-23 thomas if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
218 3efd8e31 2022-10-23 thomas size_t len;
219 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(outbuf, &len,
220 3efd8e31 2022-10-23 thomas obj, pack);
222 3efd8e31 2022-10-23 thomas goto done;
223 3efd8e31 2022-10-23 thomas *size = (off_t)len;
226 3efd8e31 2022-10-23 thomas * XXX This uses 3 file extra descriptors for no good reason.
227 3efd8e31 2022-10-23 thomas * We should have got_packfile_extract_object_to_fd().
229 3efd8e31 2022-10-23 thomas err = wrap_fd(&outfile, outfd);
231 3efd8e31 2022-10-23 thomas goto done;
232 3efd8e31 2022-10-23 thomas err = wrap_fd(&basefile, pack->basefd);
234 3efd8e31 2022-10-23 thomas goto done;
235 3efd8e31 2022-10-23 thomas err = wrap_fd(&accumfile, pack->accumfd);
237 3efd8e31 2022-10-23 thomas goto done;
238 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object(pack, obj, outfile, basefile,
239 3efd8e31 2022-10-23 thomas accumfile);
241 3efd8e31 2022-10-23 thomas goto done;
242 b296ac1c 2023-01-09 thomas *size = obj->size;
245 3efd8e31 2022-10-23 thomas *hdrlen = obj->hdrlen;
247 3efd8e31 2022-10-23 thomas got_object_close(obj);
248 3efd8e31 2022-10-23 thomas if (outfile && fclose(outfile) == EOF && err == NULL)
249 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
250 3efd8e31 2022-10-23 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
251 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
252 3efd8e31 2022-10-23 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
253 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
254 3efd8e31 2022-10-23 thomas return err;
258 3efd8e31 2022-10-23 thomas static void
259 3efd8e31 2022-10-23 thomas put_raw_object_tempfile(struct got_raw_object *obj)
261 3efd8e31 2022-10-23 thomas struct got_repository *repo = obj->close_arg;
263 3efd8e31 2022-10-23 thomas if (obj->tempfile_idx != -1)
264 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(obj->tempfile_idx, repo);
267 3efd8e31 2022-10-23 thomas /* *outfd must be initialized to -1 by caller */
268 3efd8e31 2022-10-23 thomas const struct got_error *
269 3efd8e31 2022-10-23 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
270 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
272 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
273 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
274 ab9f7b87 2023-01-09 thomas int idx, tempfd, tempfile_idx;
275 3efd8e31 2022-10-23 thomas uint8_t *outbuf = NULL;
276 3efd8e31 2022-10-23 thomas off_t size = 0;
277 3efd8e31 2022-10-23 thomas size_t hdrlen = 0;
278 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
280 3efd8e31 2022-10-23 thomas *obj = got_repo_get_cached_raw_object(repo, id);
281 3efd8e31 2022-10-23 thomas if (*obj != NULL) {
282 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
283 3efd8e31 2022-10-23 thomas return NULL;
286 ab9f7b87 2023-01-09 thomas err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
288 ab9f7b87 2023-01-09 thomas return err;
290 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
291 3efd8e31 2022-10-23 thomas if (err == NULL) {
292 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
294 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
295 3efd8e31 2022-10-23 thomas packidx->path_packidx);
297 3efd8e31 2022-10-23 thomas goto done;
299 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
300 3efd8e31 2022-10-23 thomas if (pack == NULL) {
301 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
302 3efd8e31 2022-10-23 thomas packidx);
304 3efd8e31 2022-10-23 thomas goto done;
306 3efd8e31 2022-10-23 thomas err = read_packed_object_raw(&outbuf, &size, &hdrlen,
307 ab9f7b87 2023-01-09 thomas tempfd, pack, packidx, idx, id);
309 3efd8e31 2022-10-23 thomas goto done;
310 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
313 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
315 3efd8e31 2022-10-23 thomas goto done;
316 3efd8e31 2022-10-23 thomas err = got_object_read_raw(&outbuf, &size, &hdrlen,
317 ab9f7b87 2023-01-09 thomas GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
318 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
319 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
321 ab9f7b87 2023-01-09 thomas goto done;
324 ab9f7b87 2023-01-09 thomas if (outbuf == NULL) {
325 ab9f7b87 2023-01-09 thomas if (*outfd != -1) {
326 ab9f7b87 2023-01-09 thomas err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
327 ab9f7b87 2023-01-09 thomas goto done;
331 ab9f7b87 2023-01-09 thomas * Duplicate tempfile descriptor to allow use of
332 ab9f7b87 2023-01-09 thomas * fdopen(3) inside got_object_raw_alloc().
334 ab9f7b87 2023-01-09 thomas *outfd = dup(tempfd);
335 ab9f7b87 2023-01-09 thomas if (*outfd == -1) {
336 ab9f7b87 2023-01-09 thomas err = got_error_from_errno("dup");
337 3efd8e31 2022-10-23 thomas goto done;
341 ebe6ec92 2023-01-09 thomas err = got_object_raw_alloc(obj, outbuf, outfd,
342 ebe6ec92 2023-01-09 thomas GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
344 3efd8e31 2022-10-23 thomas goto done;
346 3efd8e31 2022-10-23 thomas err = got_repo_cache_raw_object(repo, id, *obj);
347 3efd8e31 2022-10-23 thomas if (err) {
348 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
349 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
350 3efd8e31 2022-10-23 thomas err = NULL;
353 3efd8e31 2022-10-23 thomas free(path_packfile);
354 3efd8e31 2022-10-23 thomas if (err) {
355 3efd8e31 2022-10-23 thomas if (*obj) {
356 3efd8e31 2022-10-23 thomas got_object_raw_close(*obj);
357 3efd8e31 2022-10-23 thomas *obj = NULL;
359 3efd8e31 2022-10-23 thomas free(outbuf);
360 ab9f7b87 2023-01-09 thomas got_repo_temp_fds_put(tempfile_idx, repo);
361 ab9f7b87 2023-01-09 thomas if (*outfd != -1) {
362 ab9f7b87 2023-01-09 thomas close(*outfd);
363 ab9f7b87 2023-01-09 thomas *outfd = -1;
366 ab9f7b87 2023-01-09 thomas if (((*obj)->f == NULL && (*obj)->fd == -1)) {
367 ab9f7b87 2023-01-09 thomas /* This raw object is not backed by a file. */
368 ab9f7b87 2023-01-09 thomas got_repo_temp_fds_put(tempfile_idx, repo);
369 ab9f7b87 2023-01-09 thomas if (*outfd != -1) {
370 ab9f7b87 2023-01-09 thomas close(*outfd);
371 ab9f7b87 2023-01-09 thomas *outfd = -1;
374 ab9f7b87 2023-01-09 thomas (*obj)->tempfile_idx = tempfile_idx;
375 ab9f7b87 2023-01-09 thomas (*obj)->close_cb = put_raw_object_tempfile;
376 ab9f7b87 2023-01-09 thomas (*obj)->close_arg = repo;
379 3efd8e31 2022-10-23 thomas return err;
382 3efd8e31 2022-10-23 thomas static const struct got_error *
383 3efd8e31 2022-10-23 thomas open_commit(struct got_commit_object **commit,
384 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id, int check_cache)
386 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
387 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
389 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
391 3efd8e31 2022-10-23 thomas if (check_cache) {
392 3efd8e31 2022-10-23 thomas *commit = got_repo_get_cached_commit(repo, id);
393 3efd8e31 2022-10-23 thomas if (*commit != NULL) {
394 3efd8e31 2022-10-23 thomas (*commit)->refcnt++;
395 3efd8e31 2022-10-23 thomas return NULL;
398 3efd8e31 2022-10-23 thomas *commit = NULL;
400 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
401 3efd8e31 2022-10-23 thomas if (err == NULL) {
402 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
403 3efd8e31 2022-10-23 thomas struct got_object *obj;
404 3efd8e31 2022-10-23 thomas uint8_t *buf;
405 3efd8e31 2022-10-23 thomas size_t len;
407 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
408 3efd8e31 2022-10-23 thomas packidx->path_packidx);
410 3efd8e31 2022-10-23 thomas return err;
412 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
413 3efd8e31 2022-10-23 thomas if (pack == NULL) {
414 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
415 3efd8e31 2022-10-23 thomas packidx);
417 3efd8e31 2022-10-23 thomas goto done;
419 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
421 3efd8e31 2022-10-23 thomas goto done;
422 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
423 3efd8e31 2022-10-23 thomas obj, pack);
424 3efd8e31 2022-10-23 thomas got_object_close(obj);
426 3efd8e31 2022-10-23 thomas goto done;
427 3efd8e31 2022-10-23 thomas err = got_object_parse_commit(commit, buf, len);
428 3efd8e31 2022-10-23 thomas free(buf);
429 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
432 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
434 3efd8e31 2022-10-23 thomas return err;
435 3efd8e31 2022-10-23 thomas err = got_object_read_commit(commit, fd, id, 0);
436 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
437 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
439 3efd8e31 2022-10-23 thomas return err;
442 3efd8e31 2022-10-23 thomas if (err == NULL) {
443 3efd8e31 2022-10-23 thomas (*commit)->refcnt++;
444 3efd8e31 2022-10-23 thomas err = got_repo_cache_commit(repo, id, *commit);
445 3efd8e31 2022-10-23 thomas if (err) {
446 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
447 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
448 3efd8e31 2022-10-23 thomas err = NULL;
452 3efd8e31 2022-10-23 thomas free(path_packfile);
453 3efd8e31 2022-10-23 thomas return err;
456 3efd8e31 2022-10-23 thomas const struct got_error *
457 3efd8e31 2022-10-23 thomas got_object_open_as_commit(struct got_commit_object **commit,
458 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
460 3efd8e31 2022-10-23 thomas *commit = got_repo_get_cached_commit(repo, id);
461 3efd8e31 2022-10-23 thomas if (*commit != NULL) {
462 3efd8e31 2022-10-23 thomas (*commit)->refcnt++;
463 3efd8e31 2022-10-23 thomas return NULL;
466 3efd8e31 2022-10-23 thomas return open_commit(commit, repo, id, 0);
469 3efd8e31 2022-10-23 thomas const struct got_error *
470 3efd8e31 2022-10-23 thomas got_object_commit_open(struct got_commit_object **commit,
471 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj)
473 3efd8e31 2022-10-23 thomas return open_commit(commit, repo, got_object_get_id(obj), 1);
476 3efd8e31 2022-10-23 thomas static const struct got_error *
477 3efd8e31 2022-10-23 thomas open_tree(struct got_tree_object **tree,
478 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id, int check_cache)
480 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
481 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
483 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
484 3efd8e31 2022-10-23 thomas struct got_parsed_tree_entry *entries = NULL;
485 3efd8e31 2022-10-23 thomas size_t nentries = 0, nentries_alloc = 0, i;
486 3efd8e31 2022-10-23 thomas uint8_t *buf = NULL;
488 3efd8e31 2022-10-23 thomas if (check_cache) {
489 3efd8e31 2022-10-23 thomas *tree = got_repo_get_cached_tree(repo, id);
490 3efd8e31 2022-10-23 thomas if (*tree != NULL) {
491 3efd8e31 2022-10-23 thomas (*tree)->refcnt++;
492 3efd8e31 2022-10-23 thomas return NULL;
495 3efd8e31 2022-10-23 thomas *tree = NULL;
497 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
498 3efd8e31 2022-10-23 thomas if (err == NULL) {
499 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
500 3efd8e31 2022-10-23 thomas struct got_object *obj;
501 3efd8e31 2022-10-23 thomas size_t len;
503 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
504 3efd8e31 2022-10-23 thomas packidx->path_packidx);
506 3efd8e31 2022-10-23 thomas return err;
508 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
509 3efd8e31 2022-10-23 thomas if (pack == NULL) {
510 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
511 3efd8e31 2022-10-23 thomas packidx);
513 3efd8e31 2022-10-23 thomas goto done;
515 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
517 3efd8e31 2022-10-23 thomas goto done;
518 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
519 3efd8e31 2022-10-23 thomas obj, pack);
520 3efd8e31 2022-10-23 thomas got_object_close(obj);
522 3efd8e31 2022-10-23 thomas goto done;
523 3efd8e31 2022-10-23 thomas err = got_object_parse_tree(&entries, &nentries,
524 3efd8e31 2022-10-23 thomas &nentries_alloc, buf, len);
526 3efd8e31 2022-10-23 thomas goto done;
527 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
530 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
532 3efd8e31 2022-10-23 thomas return err;
533 3efd8e31 2022-10-23 thomas err = got_object_read_tree(&entries, &nentries,
534 3efd8e31 2022-10-23 thomas &nentries_alloc, &buf, fd, id);
535 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
536 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
538 3efd8e31 2022-10-23 thomas goto done;
540 3efd8e31 2022-10-23 thomas goto done;
542 3efd8e31 2022-10-23 thomas *tree = malloc(sizeof(**tree));
543 3efd8e31 2022-10-23 thomas if (*tree == NULL) {
544 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
545 3efd8e31 2022-10-23 thomas goto done;
547 3efd8e31 2022-10-23 thomas (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
548 3efd8e31 2022-10-23 thomas if ((*tree)->entries == NULL) {
549 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
550 3efd8e31 2022-10-23 thomas goto done;
552 3efd8e31 2022-10-23 thomas (*tree)->nentries = nentries;
553 3efd8e31 2022-10-23 thomas (*tree)->refcnt = 0;
555 3efd8e31 2022-10-23 thomas for (i = 0; i < nentries; i++) {
556 3efd8e31 2022-10-23 thomas struct got_parsed_tree_entry *pe = &entries[i];
557 3efd8e31 2022-10-23 thomas struct got_tree_entry *te = &(*tree)->entries[i];
559 3efd8e31 2022-10-23 thomas if (strlcpy(te->name, pe->name,
560 3efd8e31 2022-10-23 thomas sizeof(te->name)) >= sizeof(te->name)) {
561 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
562 3efd8e31 2022-10-23 thomas goto done;
564 3efd8e31 2022-10-23 thomas memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
565 3efd8e31 2022-10-23 thomas te->mode = pe->mode;
566 3efd8e31 2022-10-23 thomas te->idx = i;
569 3efd8e31 2022-10-23 thomas free(path_packfile);
570 3efd8e31 2022-10-23 thomas free(entries);
571 3efd8e31 2022-10-23 thomas free(buf);
572 3efd8e31 2022-10-23 thomas if (err == NULL) {
573 3efd8e31 2022-10-23 thomas (*tree)->refcnt++;
574 3efd8e31 2022-10-23 thomas err = got_repo_cache_tree(repo, id, *tree);
575 3efd8e31 2022-10-23 thomas if (err) {
576 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
577 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
578 3efd8e31 2022-10-23 thomas err = NULL;
581 3efd8e31 2022-10-23 thomas if (err) {
582 3efd8e31 2022-10-23 thomas if (*tree)
583 3efd8e31 2022-10-23 thomas free((*tree)->entries);
584 3efd8e31 2022-10-23 thomas free(*tree);
585 3efd8e31 2022-10-23 thomas *tree = NULL;
587 3efd8e31 2022-10-23 thomas return err;
590 3efd8e31 2022-10-23 thomas const struct got_error *
591 3efd8e31 2022-10-23 thomas got_object_open_as_tree(struct got_tree_object **tree,
592 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
594 3efd8e31 2022-10-23 thomas *tree = got_repo_get_cached_tree(repo, id);
595 3efd8e31 2022-10-23 thomas if (*tree != NULL) {
596 3efd8e31 2022-10-23 thomas (*tree)->refcnt++;
597 3efd8e31 2022-10-23 thomas return NULL;
600 3efd8e31 2022-10-23 thomas return open_tree(tree, repo, id, 0);
603 3efd8e31 2022-10-23 thomas const struct got_error *
604 3efd8e31 2022-10-23 thomas got_object_tree_open(struct got_tree_object **tree,
605 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj)
607 3efd8e31 2022-10-23 thomas return open_tree(tree, repo, got_object_get_id(obj), 1);
610 3efd8e31 2022-10-23 thomas const struct got_error *
611 3efd8e31 2022-10-23 thomas got_object_open_as_blob(struct got_blob_object **blob,
612 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id, size_t blocksize,
613 3efd8e31 2022-10-23 thomas int outfd)
615 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
618 3efd8e31 2022-10-23 thomas const struct got_error *
619 3efd8e31 2022-10-23 thomas got_object_blob_open(struct got_blob_object **blob,
620 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj, size_t blocksize,
621 3efd8e31 2022-10-23 thomas int outfd)
623 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
626 3efd8e31 2022-10-23 thomas static const struct got_error *
627 3efd8e31 2022-10-23 thomas open_tag(struct got_tag_object **tag, struct got_repository *repo,
628 3efd8e31 2022-10-23 thomas struct got_object_id *id, int check_cache)
630 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
631 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
633 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
634 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
635 3efd8e31 2022-10-23 thomas int obj_type = GOT_OBJ_TYPE_ANY;
637 3efd8e31 2022-10-23 thomas if (check_cache) {
638 3efd8e31 2022-10-23 thomas *tag = got_repo_get_cached_tag(repo, id);
639 3efd8e31 2022-10-23 thomas if (*tag != NULL) {
640 3efd8e31 2022-10-23 thomas (*tag)->refcnt++;
641 3efd8e31 2022-10-23 thomas return NULL;
644 3efd8e31 2022-10-23 thomas *tag = NULL;
646 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
647 3efd8e31 2022-10-23 thomas if (err == NULL) {
648 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
649 3efd8e31 2022-10-23 thomas uint8_t *buf = NULL;
650 3efd8e31 2022-10-23 thomas size_t len;
652 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
653 3efd8e31 2022-10-23 thomas packidx->path_packidx);
655 3efd8e31 2022-10-23 thomas return err;
657 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
658 3efd8e31 2022-10-23 thomas if (pack == NULL) {
659 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
660 3efd8e31 2022-10-23 thomas packidx);
662 3efd8e31 2022-10-23 thomas goto done;
665 3efd8e31 2022-10-23 thomas /* Beware of "lightweight" tags: Check object type first. */
666 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
668 3efd8e31 2022-10-23 thomas goto done;
669 3efd8e31 2022-10-23 thomas obj_type = obj->type;
670 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_TAG) {
671 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
672 3efd8e31 2022-10-23 thomas got_object_close(obj);
673 3efd8e31 2022-10-23 thomas goto done;
675 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
676 3efd8e31 2022-10-23 thomas obj, pack);
677 3efd8e31 2022-10-23 thomas got_object_close(obj);
679 3efd8e31 2022-10-23 thomas goto done;
680 3efd8e31 2022-10-23 thomas err = got_object_parse_tag(tag, buf, len);
681 3efd8e31 2022-10-23 thomas free(buf);
682 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
685 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
687 3efd8e31 2022-10-23 thomas return err;
688 3efd8e31 2022-10-23 thomas err = got_object_read_header(&obj, fd);
689 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
690 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
692 3efd8e31 2022-10-23 thomas return err;
693 3efd8e31 2022-10-23 thomas obj_type = obj->type;
694 3efd8e31 2022-10-23 thomas got_object_close(obj);
695 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_TAG)
696 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
698 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
700 3efd8e31 2022-10-23 thomas return err;
701 3efd8e31 2022-10-23 thomas err = got_object_read_tag(tag, fd, id, 0);
702 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
703 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
705 3efd8e31 2022-10-23 thomas return err;
708 3efd8e31 2022-10-23 thomas if (err == NULL) {
709 3efd8e31 2022-10-23 thomas (*tag)->refcnt++;
710 3efd8e31 2022-10-23 thomas err = got_repo_cache_tag(repo, id, *tag);
711 3efd8e31 2022-10-23 thomas if (err) {
712 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
713 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
714 3efd8e31 2022-10-23 thomas err = NULL;
718 3efd8e31 2022-10-23 thomas free(path_packfile);
719 3efd8e31 2022-10-23 thomas return err;
722 3efd8e31 2022-10-23 thomas const struct got_error *
723 3efd8e31 2022-10-23 thomas got_object_open_as_tag(struct got_tag_object **tag,
724 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
726 3efd8e31 2022-10-23 thomas *tag = got_repo_get_cached_tag(repo, id);
727 3efd8e31 2022-10-23 thomas if (*tag != NULL) {
728 3efd8e31 2022-10-23 thomas (*tag)->refcnt++;
729 3efd8e31 2022-10-23 thomas return NULL;
732 3efd8e31 2022-10-23 thomas return open_tag(tag, repo, id, 0);
735 3efd8e31 2022-10-23 thomas const struct got_error *
736 3efd8e31 2022-10-23 thomas got_object_tag_open(struct got_tag_object **tag,
737 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj)
739 3efd8e31 2022-10-23 thomas return open_tag(tag, repo, got_object_get_id(obj), 1);