Blame


1 3efd8e31 2022-10-23 thomas /*
2 3efd8e31 2022-10-23 thomas * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 3efd8e31 2022-10-23 thomas *
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.
7 3efd8e31 2022-10-23 thomas *
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.
15 3efd8e31 2022-10-23 thomas */
16 3efd8e31 2022-10-23 thomas
17 3efd8e31 2022-10-23 thomas #include <sys/queue.h>
18 3efd8e31 2022-10-23 thomas #include <sys/tree.h>
19 3efd8e31 2022-10-23 thomas #include <sys/stat.h>
20 3efd8e31 2022-10-23 thomas
21 3efd8e31 2022-10-23 thomas #include <errno.h>
22 3efd8e31 2022-10-23 thomas #include <limits.h>
23 3efd8e31 2022-10-23 thomas #include <stdio.h>
24 3efd8e31 2022-10-23 thomas #include <stdlib.h>
25 3efd8e31 2022-10-23 thomas #include <string.h>
26 3efd8e31 2022-10-23 thomas #include <unistd.h>
27 3efd8e31 2022-10-23 thomas
28 3efd8e31 2022-10-23 thomas #include "got_error.h"
29 3efd8e31 2022-10-23 thomas #include "got_object.h"
30 3efd8e31 2022-10-23 thomas #include "got_repository.h"
31 3efd8e31 2022-10-23 thomas #include "got_path.h"
32 3efd8e31 2022-10-23 thomas
33 3efd8e31 2022-10-23 thomas #include "got_lib_delta.h"
34 3efd8e31 2022-10-23 thomas #include "got_lib_object.h"
35 3efd8e31 2022-10-23 thomas #include "got_lib_object_cache.h"
36 3efd8e31 2022-10-23 thomas #include "got_lib_object_parse.h"
37 3efd8e31 2022-10-23 thomas #include "got_lib_pack.h"
38 3efd8e31 2022-10-23 thomas #include "got_lib_repository.h"
39 3efd8e31 2022-10-23 thomas
40 3efd8e31 2022-10-23 thomas const struct got_error *
41 3efd8e31 2022-10-23 thomas got_object_open_packed(struct got_object **obj, struct got_object_id *id,
42 3efd8e31 2022-10-23 thomas struct got_repository *repo)
43 3efd8e31 2022-10-23 thomas {
44 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
45 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
46 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
47 3efd8e31 2022-10-23 thomas int idx;
48 3efd8e31 2022-10-23 thomas char *path_packfile;
49 3efd8e31 2022-10-23 thomas
50 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
51 3efd8e31 2022-10-23 thomas if (err)
52 3efd8e31 2022-10-23 thomas return err;
53 3efd8e31 2022-10-23 thomas
54 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
55 3efd8e31 2022-10-23 thomas packidx->path_packidx);
56 3efd8e31 2022-10-23 thomas if (err)
57 3efd8e31 2022-10-23 thomas return err;
58 3efd8e31 2022-10-23 thomas
59 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
60 3efd8e31 2022-10-23 thomas if (pack == NULL) {
61 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
62 3efd8e31 2022-10-23 thomas if (err)
63 3efd8e31 2022-10-23 thomas goto done;
64 3efd8e31 2022-10-23 thomas }
65 3efd8e31 2022-10-23 thomas
66 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(obj, pack, packidx, idx, id);
67 3efd8e31 2022-10-23 thomas if (err)
68 3efd8e31 2022-10-23 thomas return err;
69 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
70 3efd8e31 2022-10-23 thomas
71 3efd8e31 2022-10-23 thomas err = got_repo_cache_object(repo, id, *obj);
72 3efd8e31 2022-10-23 thomas if (err) {
73 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
74 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
75 3efd8e31 2022-10-23 thomas err = NULL;
76 3efd8e31 2022-10-23 thomas }
77 3efd8e31 2022-10-23 thomas done:
78 3efd8e31 2022-10-23 thomas free(path_packfile);
79 3efd8e31 2022-10-23 thomas return err;
80 3efd8e31 2022-10-23 thomas }
81 3efd8e31 2022-10-23 thomas
82 3efd8e31 2022-10-23 thomas const struct got_error *
83 3efd8e31 2022-10-23 thomas got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
84 3efd8e31 2022-10-23 thomas struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
85 3efd8e31 2022-10-23 thomas struct got_repository *repo)
86 3efd8e31 2022-10-23 thomas {
87 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
88 3efd8e31 2022-10-23 thomas }
89 3efd8e31 2022-10-23 thomas
90 3efd8e31 2022-10-23 thomas const struct got_error *
91 3efd8e31 2022-10-23 thomas got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
92 3efd8e31 2022-10-23 thomas off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
93 3efd8e31 2022-10-23 thomas off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
94 3efd8e31 2022-10-23 thomas struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
95 3efd8e31 2022-10-23 thomas struct got_repository *repo)
96 3efd8e31 2022-10-23 thomas {
97 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
98 3efd8e31 2022-10-23 thomas }
99 3efd8e31 2022-10-23 thomas
100 3efd8e31 2022-10-23 thomas const struct got_error *
101 3efd8e31 2022-10-23 thomas got_object_open(struct got_object **obj, struct got_repository *repo,
102 3efd8e31 2022-10-23 thomas struct got_object_id *id)
103 3efd8e31 2022-10-23 thomas {
104 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
105 3efd8e31 2022-10-23 thomas int fd;
106 3efd8e31 2022-10-23 thomas
107 3efd8e31 2022-10-23 thomas *obj = got_repo_get_cached_object(repo, id);
108 3efd8e31 2022-10-23 thomas if (*obj != NULL) {
109 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
110 3efd8e31 2022-10-23 thomas return NULL;
111 3efd8e31 2022-10-23 thomas }
112 3efd8e31 2022-10-23 thomas
113 3efd8e31 2022-10-23 thomas err = got_object_open_packed(obj, id, repo);
114 3efd8e31 2022-10-23 thomas if (err) {
115 3efd8e31 2022-10-23 thomas if (err->code != GOT_ERR_NO_OBJ)
116 3efd8e31 2022-10-23 thomas return err;
117 3efd8e31 2022-10-23 thomas } else
118 3efd8e31 2022-10-23 thomas return NULL;
119 3efd8e31 2022-10-23 thomas
120 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
121 3efd8e31 2022-10-23 thomas if (err) {
122 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
123 3efd8e31 2022-10-23 thomas err = got_error_no_obj(id);
124 3efd8e31 2022-10-23 thomas return err;
125 3efd8e31 2022-10-23 thomas }
126 3efd8e31 2022-10-23 thomas
127 3efd8e31 2022-10-23 thomas err = got_object_read_header(obj, fd);
128 3efd8e31 2022-10-23 thomas if (err)
129 3efd8e31 2022-10-23 thomas goto done;
130 3efd8e31 2022-10-23 thomas
131 3efd8e31 2022-10-23 thomas memcpy(&(*obj)->id, id, sizeof((*obj)->id));
132 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
133 3efd8e31 2022-10-23 thomas
134 3efd8e31 2022-10-23 thomas err = got_repo_cache_object(repo, id, *obj);
135 3efd8e31 2022-10-23 thomas if (err) {
136 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
137 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
138 3efd8e31 2022-10-23 thomas err = NULL;
139 3efd8e31 2022-10-23 thomas }
140 3efd8e31 2022-10-23 thomas done:
141 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
142 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
143 3efd8e31 2022-10-23 thomas return err;
144 3efd8e31 2022-10-23 thomas }
145 3efd8e31 2022-10-23 thomas
146 3efd8e31 2022-10-23 thomas static const struct got_error *
147 3efd8e31 2022-10-23 thomas wrap_fd(FILE **f, int wrapped_fd)
148 3efd8e31 2022-10-23 thomas {
149 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
150 3efd8e31 2022-10-23 thomas int fd;
151 3efd8e31 2022-10-23 thomas
152 3efd8e31 2022-10-23 thomas if (ftruncate(wrapped_fd, 0L) == -1)
153 3efd8e31 2022-10-23 thomas return got_error_from_errno("ftruncate");
154 3efd8e31 2022-10-23 thomas
155 3efd8e31 2022-10-23 thomas if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
156 3efd8e31 2022-10-23 thomas return got_error_from_errno("lseek");
157 3efd8e31 2022-10-23 thomas
158 3efd8e31 2022-10-23 thomas fd = dup(wrapped_fd);
159 3efd8e31 2022-10-23 thomas if (fd == -1)
160 3efd8e31 2022-10-23 thomas return got_error_from_errno("dup");
161 3efd8e31 2022-10-23 thomas
162 3efd8e31 2022-10-23 thomas *f = fdopen(fd, "w+");
163 3efd8e31 2022-10-23 thomas if (*f == NULL) {
164 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fdopen");
165 3efd8e31 2022-10-23 thomas close(fd);
166 3efd8e31 2022-10-23 thomas }
167 3efd8e31 2022-10-23 thomas return err;
168 3efd8e31 2022-10-23 thomas }
169 3efd8e31 2022-10-23 thomas
170 3efd8e31 2022-10-23 thomas static const struct got_error *
171 3efd8e31 2022-10-23 thomas read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
172 3efd8e31 2022-10-23 thomas int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
173 3efd8e31 2022-10-23 thomas struct got_object_id *id)
174 3efd8e31 2022-10-23 thomas {
175 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
176 3efd8e31 2022-10-23 thomas uint64_t raw_size = 0;
177 3efd8e31 2022-10-23 thomas struct got_object *obj;
178 3efd8e31 2022-10-23 thomas FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
179 3efd8e31 2022-10-23 thomas
180 3efd8e31 2022-10-23 thomas *outbuf = NULL;
181 3efd8e31 2022-10-23 thomas *size = 0;
182 3efd8e31 2022-10-23 thomas *hdrlen = 0;
183 3efd8e31 2022-10-23 thomas
184 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
185 3efd8e31 2022-10-23 thomas if (err)
186 3efd8e31 2022-10-23 thomas return err;
187 3efd8e31 2022-10-23 thomas
188 3efd8e31 2022-10-23 thomas if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
189 3efd8e31 2022-10-23 thomas err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
190 3efd8e31 2022-10-23 thomas if (err)
191 3efd8e31 2022-10-23 thomas goto done;
192 3efd8e31 2022-10-23 thomas } else
193 3efd8e31 2022-10-23 thomas raw_size = obj->size;
194 3efd8e31 2022-10-23 thomas
195 3efd8e31 2022-10-23 thomas if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
196 3efd8e31 2022-10-23 thomas size_t len;
197 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(outbuf, &len,
198 3efd8e31 2022-10-23 thomas obj, pack);
199 3efd8e31 2022-10-23 thomas if (err)
200 3efd8e31 2022-10-23 thomas goto done;
201 3efd8e31 2022-10-23 thomas *size = (off_t)len;
202 3efd8e31 2022-10-23 thomas } else {
203 3efd8e31 2022-10-23 thomas /*
204 3efd8e31 2022-10-23 thomas * XXX This uses 3 file extra descriptors for no good reason.
205 3efd8e31 2022-10-23 thomas * We should have got_packfile_extract_object_to_fd().
206 3efd8e31 2022-10-23 thomas */
207 3efd8e31 2022-10-23 thomas err = wrap_fd(&outfile, outfd);
208 3efd8e31 2022-10-23 thomas if (err)
209 3efd8e31 2022-10-23 thomas goto done;
210 3efd8e31 2022-10-23 thomas err = wrap_fd(&basefile, pack->basefd);
211 3efd8e31 2022-10-23 thomas if (err)
212 3efd8e31 2022-10-23 thomas goto done;
213 3efd8e31 2022-10-23 thomas err = wrap_fd(&accumfile, pack->accumfd);
214 3efd8e31 2022-10-23 thomas if (err)
215 3efd8e31 2022-10-23 thomas goto done;
216 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object(pack, obj, outfile, basefile,
217 3efd8e31 2022-10-23 thomas accumfile);
218 3efd8e31 2022-10-23 thomas if (err)
219 3efd8e31 2022-10-23 thomas goto done;
220 b296ac1c 2023-01-09 thomas *size = obj->size;
221 3efd8e31 2022-10-23 thomas }
222 3efd8e31 2022-10-23 thomas
223 3efd8e31 2022-10-23 thomas *hdrlen = obj->hdrlen;
224 3efd8e31 2022-10-23 thomas done:
225 3efd8e31 2022-10-23 thomas got_object_close(obj);
226 3efd8e31 2022-10-23 thomas if (outfile && fclose(outfile) == EOF && err == NULL)
227 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
228 3efd8e31 2022-10-23 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
229 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
230 3efd8e31 2022-10-23 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
231 3efd8e31 2022-10-23 thomas err = got_error_from_errno("fclose");
232 3efd8e31 2022-10-23 thomas return err;
233 3efd8e31 2022-10-23 thomas
234 3efd8e31 2022-10-23 thomas }
235 3efd8e31 2022-10-23 thomas
236 3efd8e31 2022-10-23 thomas static void
237 3efd8e31 2022-10-23 thomas put_raw_object_tempfile(struct got_raw_object *obj)
238 3efd8e31 2022-10-23 thomas {
239 3efd8e31 2022-10-23 thomas struct got_repository *repo = obj->close_arg;
240 3efd8e31 2022-10-23 thomas
241 3efd8e31 2022-10-23 thomas if (obj->tempfile_idx != -1)
242 3efd8e31 2022-10-23 thomas got_repo_temp_fds_put(obj->tempfile_idx, repo);
243 3efd8e31 2022-10-23 thomas }
244 3efd8e31 2022-10-23 thomas
245 3efd8e31 2022-10-23 thomas /* *outfd must be initialized to -1 by caller */
246 3efd8e31 2022-10-23 thomas const struct got_error *
247 3efd8e31 2022-10-23 thomas got_object_raw_open(struct got_raw_object **obj, int *outfd,
248 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
249 3efd8e31 2022-10-23 thomas {
250 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
251 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
252 ab9f7b87 2023-01-09 thomas int idx, tempfd, tempfile_idx;
253 3efd8e31 2022-10-23 thomas uint8_t *outbuf = NULL;
254 3efd8e31 2022-10-23 thomas off_t size = 0;
255 3efd8e31 2022-10-23 thomas size_t hdrlen = 0;
256 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
257 3efd8e31 2022-10-23 thomas
258 3efd8e31 2022-10-23 thomas *obj = got_repo_get_cached_raw_object(repo, id);
259 3efd8e31 2022-10-23 thomas if (*obj != NULL) {
260 3efd8e31 2022-10-23 thomas (*obj)->refcnt++;
261 3efd8e31 2022-10-23 thomas return NULL;
262 3efd8e31 2022-10-23 thomas }
263 3efd8e31 2022-10-23 thomas
264 ab9f7b87 2023-01-09 thomas err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
265 ab9f7b87 2023-01-09 thomas if (err)
266 ab9f7b87 2023-01-09 thomas return err;
267 3efd8e31 2022-10-23 thomas
268 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
269 3efd8e31 2022-10-23 thomas if (err == NULL) {
270 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
271 3efd8e31 2022-10-23 thomas
272 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
273 3efd8e31 2022-10-23 thomas packidx->path_packidx);
274 3efd8e31 2022-10-23 thomas if (err)
275 3efd8e31 2022-10-23 thomas goto done;
276 3efd8e31 2022-10-23 thomas
277 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
278 3efd8e31 2022-10-23 thomas if (pack == NULL) {
279 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
280 3efd8e31 2022-10-23 thomas packidx);
281 3efd8e31 2022-10-23 thomas if (err)
282 3efd8e31 2022-10-23 thomas goto done;
283 3efd8e31 2022-10-23 thomas }
284 3efd8e31 2022-10-23 thomas err = read_packed_object_raw(&outbuf, &size, &hdrlen,
285 ab9f7b87 2023-01-09 thomas tempfd, pack, packidx, idx, id);
286 3efd8e31 2022-10-23 thomas if (err)
287 3efd8e31 2022-10-23 thomas goto done;
288 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
289 3efd8e31 2022-10-23 thomas int fd;
290 3efd8e31 2022-10-23 thomas
291 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
292 3efd8e31 2022-10-23 thomas if (err)
293 3efd8e31 2022-10-23 thomas goto done;
294 3efd8e31 2022-10-23 thomas err = got_object_read_raw(&outbuf, &size, &hdrlen,
295 ab9f7b87 2023-01-09 thomas GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
296 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
297 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
298 3efd8e31 2022-10-23 thomas if (err)
299 ab9f7b87 2023-01-09 thomas goto done;
300 ab9f7b87 2023-01-09 thomas }
301 ab9f7b87 2023-01-09 thomas
302 ab9f7b87 2023-01-09 thomas if (outbuf == NULL) {
303 ab9f7b87 2023-01-09 thomas if (*outfd != -1) {
304 ab9f7b87 2023-01-09 thomas err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
305 ab9f7b87 2023-01-09 thomas goto done;
306 ab9f7b87 2023-01-09 thomas }
307 ab9f7b87 2023-01-09 thomas
308 ab9f7b87 2023-01-09 thomas /*
309 ab9f7b87 2023-01-09 thomas * Duplicate tempfile descriptor to allow use of
310 ab9f7b87 2023-01-09 thomas * fdopen(3) inside got_object_raw_alloc().
311 ab9f7b87 2023-01-09 thomas */
312 ab9f7b87 2023-01-09 thomas *outfd = dup(tempfd);
313 ab9f7b87 2023-01-09 thomas if (*outfd == -1) {
314 ab9f7b87 2023-01-09 thomas err = got_error_from_errno("dup");
315 3efd8e31 2022-10-23 thomas goto done;
316 ab9f7b87 2023-01-09 thomas }
317 3efd8e31 2022-10-23 thomas }
318 3efd8e31 2022-10-23 thomas
319 ebe6ec92 2023-01-09 thomas err = got_object_raw_alloc(obj, outbuf, outfd,
320 ebe6ec92 2023-01-09 thomas GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
321 3efd8e31 2022-10-23 thomas if (err)
322 3efd8e31 2022-10-23 thomas goto done;
323 3efd8e31 2022-10-23 thomas
324 3efd8e31 2022-10-23 thomas err = got_repo_cache_raw_object(repo, id, *obj);
325 3efd8e31 2022-10-23 thomas if (err) {
326 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
327 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
328 3efd8e31 2022-10-23 thomas err = NULL;
329 3efd8e31 2022-10-23 thomas }
330 3efd8e31 2022-10-23 thomas done:
331 3efd8e31 2022-10-23 thomas free(path_packfile);
332 3efd8e31 2022-10-23 thomas if (err) {
333 3efd8e31 2022-10-23 thomas if (*obj) {
334 3efd8e31 2022-10-23 thomas got_object_raw_close(*obj);
335 3efd8e31 2022-10-23 thomas *obj = NULL;
336 3efd8e31 2022-10-23 thomas }
337 3efd8e31 2022-10-23 thomas free(outbuf);
338 ab9f7b87 2023-01-09 thomas got_repo_temp_fds_put(tempfile_idx, repo);
339 ab9f7b87 2023-01-09 thomas if (*outfd != -1) {
340 ab9f7b87 2023-01-09 thomas close(*outfd);
341 ab9f7b87 2023-01-09 thomas *outfd = -1;
342 ab9f7b87 2023-01-09 thomas }
343 3efd8e31 2022-10-23 thomas } else {
344 ab9f7b87 2023-01-09 thomas if (((*obj)->f == NULL && (*obj)->fd == -1)) {
345 ab9f7b87 2023-01-09 thomas /* This raw object is not backed by a file. */
346 ab9f7b87 2023-01-09 thomas got_repo_temp_fds_put(tempfile_idx, repo);
347 ab9f7b87 2023-01-09 thomas if (*outfd != -1) {
348 ab9f7b87 2023-01-09 thomas close(*outfd);
349 ab9f7b87 2023-01-09 thomas *outfd = -1;
350 ab9f7b87 2023-01-09 thomas }
351 ab9f7b87 2023-01-09 thomas } else {
352 ab9f7b87 2023-01-09 thomas (*obj)->tempfile_idx = tempfile_idx;
353 ab9f7b87 2023-01-09 thomas (*obj)->close_cb = put_raw_object_tempfile;
354 ab9f7b87 2023-01-09 thomas (*obj)->close_arg = repo;
355 ab9f7b87 2023-01-09 thomas }
356 3efd8e31 2022-10-23 thomas }
357 3efd8e31 2022-10-23 thomas return err;
358 3efd8e31 2022-10-23 thomas }
359 3efd8e31 2022-10-23 thomas
360 3efd8e31 2022-10-23 thomas static const struct got_error *
361 3efd8e31 2022-10-23 thomas open_commit(struct got_commit_object **commit,
362 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id, int check_cache)
363 3efd8e31 2022-10-23 thomas {
364 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
365 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
366 3efd8e31 2022-10-23 thomas int idx;
367 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
368 3efd8e31 2022-10-23 thomas
369 3efd8e31 2022-10-23 thomas if (check_cache) {
370 3efd8e31 2022-10-23 thomas *commit = got_repo_get_cached_commit(repo, id);
371 3efd8e31 2022-10-23 thomas if (*commit != NULL) {
372 3efd8e31 2022-10-23 thomas (*commit)->refcnt++;
373 3efd8e31 2022-10-23 thomas return NULL;
374 3efd8e31 2022-10-23 thomas }
375 3efd8e31 2022-10-23 thomas } else
376 3efd8e31 2022-10-23 thomas *commit = NULL;
377 3efd8e31 2022-10-23 thomas
378 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
379 3efd8e31 2022-10-23 thomas if (err == NULL) {
380 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
381 3efd8e31 2022-10-23 thomas struct got_object *obj;
382 3efd8e31 2022-10-23 thomas uint8_t *buf;
383 3efd8e31 2022-10-23 thomas size_t len;
384 3efd8e31 2022-10-23 thomas
385 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
386 3efd8e31 2022-10-23 thomas packidx->path_packidx);
387 3efd8e31 2022-10-23 thomas if (err)
388 3efd8e31 2022-10-23 thomas return err;
389 3efd8e31 2022-10-23 thomas
390 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
391 3efd8e31 2022-10-23 thomas if (pack == NULL) {
392 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
393 3efd8e31 2022-10-23 thomas packidx);
394 3efd8e31 2022-10-23 thomas if (err)
395 3efd8e31 2022-10-23 thomas goto done;
396 3efd8e31 2022-10-23 thomas }
397 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
398 3efd8e31 2022-10-23 thomas if (err)
399 3efd8e31 2022-10-23 thomas goto done;
400 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
401 3efd8e31 2022-10-23 thomas obj, pack);
402 3efd8e31 2022-10-23 thomas got_object_close(obj);
403 3efd8e31 2022-10-23 thomas if (err)
404 3efd8e31 2022-10-23 thomas goto done;
405 3efd8e31 2022-10-23 thomas err = got_object_parse_commit(commit, buf, len);
406 3efd8e31 2022-10-23 thomas free(buf);
407 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
408 3efd8e31 2022-10-23 thomas int fd;
409 3efd8e31 2022-10-23 thomas
410 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
411 3efd8e31 2022-10-23 thomas if (err)
412 3efd8e31 2022-10-23 thomas return err;
413 3efd8e31 2022-10-23 thomas err = got_object_read_commit(commit, fd, id, 0);
414 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
415 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
416 3efd8e31 2022-10-23 thomas if (err)
417 3efd8e31 2022-10-23 thomas return err;
418 3efd8e31 2022-10-23 thomas }
419 3efd8e31 2022-10-23 thomas
420 3efd8e31 2022-10-23 thomas if (err == NULL) {
421 3efd8e31 2022-10-23 thomas (*commit)->refcnt++;
422 3efd8e31 2022-10-23 thomas err = got_repo_cache_commit(repo, id, *commit);
423 3efd8e31 2022-10-23 thomas if (err) {
424 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
425 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
426 3efd8e31 2022-10-23 thomas err = NULL;
427 3efd8e31 2022-10-23 thomas }
428 3efd8e31 2022-10-23 thomas }
429 3efd8e31 2022-10-23 thomas done:
430 3efd8e31 2022-10-23 thomas free(path_packfile);
431 3efd8e31 2022-10-23 thomas return err;
432 3efd8e31 2022-10-23 thomas }
433 3efd8e31 2022-10-23 thomas
434 3efd8e31 2022-10-23 thomas const struct got_error *
435 3efd8e31 2022-10-23 thomas got_object_open_as_commit(struct got_commit_object **commit,
436 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
437 3efd8e31 2022-10-23 thomas {
438 3efd8e31 2022-10-23 thomas *commit = got_repo_get_cached_commit(repo, id);
439 3efd8e31 2022-10-23 thomas if (*commit != NULL) {
440 3efd8e31 2022-10-23 thomas (*commit)->refcnt++;
441 3efd8e31 2022-10-23 thomas return NULL;
442 3efd8e31 2022-10-23 thomas }
443 3efd8e31 2022-10-23 thomas
444 3efd8e31 2022-10-23 thomas return open_commit(commit, repo, id, 0);
445 3efd8e31 2022-10-23 thomas }
446 3efd8e31 2022-10-23 thomas
447 3efd8e31 2022-10-23 thomas const struct got_error *
448 3efd8e31 2022-10-23 thomas got_object_commit_open(struct got_commit_object **commit,
449 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj)
450 3efd8e31 2022-10-23 thomas {
451 3efd8e31 2022-10-23 thomas return open_commit(commit, repo, got_object_get_id(obj), 1);
452 3efd8e31 2022-10-23 thomas }
453 3efd8e31 2022-10-23 thomas
454 3efd8e31 2022-10-23 thomas static const struct got_error *
455 3efd8e31 2022-10-23 thomas open_tree(struct got_tree_object **tree,
456 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id, int check_cache)
457 3efd8e31 2022-10-23 thomas {
458 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
459 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
460 3efd8e31 2022-10-23 thomas int idx;
461 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
462 3efd8e31 2022-10-23 thomas struct got_parsed_tree_entry *entries = NULL;
463 3efd8e31 2022-10-23 thomas size_t nentries = 0, nentries_alloc = 0, i;
464 3efd8e31 2022-10-23 thomas uint8_t *buf = NULL;
465 3efd8e31 2022-10-23 thomas
466 3efd8e31 2022-10-23 thomas if (check_cache) {
467 3efd8e31 2022-10-23 thomas *tree = got_repo_get_cached_tree(repo, id);
468 3efd8e31 2022-10-23 thomas if (*tree != NULL) {
469 3efd8e31 2022-10-23 thomas (*tree)->refcnt++;
470 3efd8e31 2022-10-23 thomas return NULL;
471 3efd8e31 2022-10-23 thomas }
472 3efd8e31 2022-10-23 thomas } else
473 3efd8e31 2022-10-23 thomas *tree = NULL;
474 3efd8e31 2022-10-23 thomas
475 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
476 3efd8e31 2022-10-23 thomas if (err == NULL) {
477 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
478 3efd8e31 2022-10-23 thomas struct got_object *obj;
479 3efd8e31 2022-10-23 thomas size_t len;
480 3efd8e31 2022-10-23 thomas
481 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
482 3efd8e31 2022-10-23 thomas packidx->path_packidx);
483 3efd8e31 2022-10-23 thomas if (err)
484 3efd8e31 2022-10-23 thomas return err;
485 3efd8e31 2022-10-23 thomas
486 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
487 3efd8e31 2022-10-23 thomas if (pack == NULL) {
488 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
489 3efd8e31 2022-10-23 thomas packidx);
490 3efd8e31 2022-10-23 thomas if (err)
491 3efd8e31 2022-10-23 thomas goto done;
492 3efd8e31 2022-10-23 thomas }
493 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
494 3efd8e31 2022-10-23 thomas if (err)
495 3efd8e31 2022-10-23 thomas goto done;
496 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
497 3efd8e31 2022-10-23 thomas obj, pack);
498 3efd8e31 2022-10-23 thomas got_object_close(obj);
499 3efd8e31 2022-10-23 thomas if (err)
500 3efd8e31 2022-10-23 thomas goto done;
501 3efd8e31 2022-10-23 thomas err = got_object_parse_tree(&entries, &nentries,
502 3efd8e31 2022-10-23 thomas &nentries_alloc, buf, len);
503 3efd8e31 2022-10-23 thomas if (err)
504 3efd8e31 2022-10-23 thomas goto done;
505 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
506 3efd8e31 2022-10-23 thomas int fd;
507 3efd8e31 2022-10-23 thomas
508 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
509 3efd8e31 2022-10-23 thomas if (err)
510 3efd8e31 2022-10-23 thomas return err;
511 3efd8e31 2022-10-23 thomas err = got_object_read_tree(&entries, &nentries,
512 3efd8e31 2022-10-23 thomas &nentries_alloc, &buf, fd, id);
513 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
514 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
515 3efd8e31 2022-10-23 thomas if (err)
516 3efd8e31 2022-10-23 thomas goto done;
517 3efd8e31 2022-10-23 thomas } else
518 3efd8e31 2022-10-23 thomas goto done;
519 3efd8e31 2022-10-23 thomas
520 3efd8e31 2022-10-23 thomas *tree = malloc(sizeof(**tree));
521 3efd8e31 2022-10-23 thomas if (*tree == NULL) {
522 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
523 3efd8e31 2022-10-23 thomas goto done;
524 3efd8e31 2022-10-23 thomas }
525 3efd8e31 2022-10-23 thomas (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
526 3efd8e31 2022-10-23 thomas if ((*tree)->entries == NULL) {
527 3efd8e31 2022-10-23 thomas err = got_error_from_errno("malloc");
528 3efd8e31 2022-10-23 thomas goto done;
529 3efd8e31 2022-10-23 thomas }
530 3efd8e31 2022-10-23 thomas (*tree)->nentries = nentries;
531 3efd8e31 2022-10-23 thomas (*tree)->refcnt = 0;
532 3efd8e31 2022-10-23 thomas
533 3efd8e31 2022-10-23 thomas for (i = 0; i < nentries; i++) {
534 3efd8e31 2022-10-23 thomas struct got_parsed_tree_entry *pe = &entries[i];
535 3efd8e31 2022-10-23 thomas struct got_tree_entry *te = &(*tree)->entries[i];
536 3efd8e31 2022-10-23 thomas
537 3efd8e31 2022-10-23 thomas if (strlcpy(te->name, pe->name,
538 3efd8e31 2022-10-23 thomas sizeof(te->name)) >= sizeof(te->name)) {
539 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_NO_SPACE);
540 3efd8e31 2022-10-23 thomas goto done;
541 3efd8e31 2022-10-23 thomas }
542 3efd8e31 2022-10-23 thomas memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
543 3efd8e31 2022-10-23 thomas te->mode = pe->mode;
544 3efd8e31 2022-10-23 thomas te->idx = i;
545 3efd8e31 2022-10-23 thomas }
546 3efd8e31 2022-10-23 thomas done:
547 3efd8e31 2022-10-23 thomas free(path_packfile);
548 3efd8e31 2022-10-23 thomas free(entries);
549 3efd8e31 2022-10-23 thomas free(buf);
550 3efd8e31 2022-10-23 thomas if (err == NULL) {
551 3efd8e31 2022-10-23 thomas (*tree)->refcnt++;
552 3efd8e31 2022-10-23 thomas err = got_repo_cache_tree(repo, id, *tree);
553 3efd8e31 2022-10-23 thomas if (err) {
554 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
555 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
556 3efd8e31 2022-10-23 thomas err = NULL;
557 3efd8e31 2022-10-23 thomas }
558 3efd8e31 2022-10-23 thomas }
559 3efd8e31 2022-10-23 thomas if (err) {
560 3efd8e31 2022-10-23 thomas if (*tree)
561 3efd8e31 2022-10-23 thomas free((*tree)->entries);
562 3efd8e31 2022-10-23 thomas free(*tree);
563 3efd8e31 2022-10-23 thomas *tree = NULL;
564 3efd8e31 2022-10-23 thomas }
565 3efd8e31 2022-10-23 thomas return err;
566 3efd8e31 2022-10-23 thomas }
567 3efd8e31 2022-10-23 thomas
568 3efd8e31 2022-10-23 thomas const struct got_error *
569 3efd8e31 2022-10-23 thomas got_object_open_as_tree(struct got_tree_object **tree,
570 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
571 3efd8e31 2022-10-23 thomas {
572 3efd8e31 2022-10-23 thomas *tree = got_repo_get_cached_tree(repo, id);
573 3efd8e31 2022-10-23 thomas if (*tree != NULL) {
574 3efd8e31 2022-10-23 thomas (*tree)->refcnt++;
575 3efd8e31 2022-10-23 thomas return NULL;
576 3efd8e31 2022-10-23 thomas }
577 3efd8e31 2022-10-23 thomas
578 3efd8e31 2022-10-23 thomas return open_tree(tree, repo, id, 0);
579 3efd8e31 2022-10-23 thomas }
580 3efd8e31 2022-10-23 thomas
581 3efd8e31 2022-10-23 thomas const struct got_error *
582 3efd8e31 2022-10-23 thomas got_object_tree_open(struct got_tree_object **tree,
583 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj)
584 3efd8e31 2022-10-23 thomas {
585 3efd8e31 2022-10-23 thomas return open_tree(tree, repo, got_object_get_id(obj), 1);
586 3efd8e31 2022-10-23 thomas }
587 3efd8e31 2022-10-23 thomas
588 3efd8e31 2022-10-23 thomas const struct got_error *
589 3efd8e31 2022-10-23 thomas got_object_open_as_blob(struct got_blob_object **blob,
590 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id, size_t blocksize,
591 3efd8e31 2022-10-23 thomas int outfd)
592 3efd8e31 2022-10-23 thomas {
593 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
594 3efd8e31 2022-10-23 thomas }
595 3efd8e31 2022-10-23 thomas
596 3efd8e31 2022-10-23 thomas const struct got_error *
597 3efd8e31 2022-10-23 thomas got_object_blob_open(struct got_blob_object **blob,
598 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj, size_t blocksize,
599 3efd8e31 2022-10-23 thomas int outfd)
600 3efd8e31 2022-10-23 thomas {
601 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_NOT_IMPL);
602 3efd8e31 2022-10-23 thomas }
603 3efd8e31 2022-10-23 thomas
604 3efd8e31 2022-10-23 thomas static const struct got_error *
605 3efd8e31 2022-10-23 thomas open_tag(struct got_tag_object **tag, struct got_repository *repo,
606 3efd8e31 2022-10-23 thomas struct got_object_id *id, int check_cache)
607 3efd8e31 2022-10-23 thomas {
608 3efd8e31 2022-10-23 thomas const struct got_error *err = NULL;
609 3efd8e31 2022-10-23 thomas struct got_packidx *packidx = NULL;
610 3efd8e31 2022-10-23 thomas int idx;
611 3efd8e31 2022-10-23 thomas char *path_packfile = NULL;
612 3efd8e31 2022-10-23 thomas struct got_object *obj = NULL;
613 3efd8e31 2022-10-23 thomas int obj_type = GOT_OBJ_TYPE_ANY;
614 3efd8e31 2022-10-23 thomas
615 3efd8e31 2022-10-23 thomas if (check_cache) {
616 3efd8e31 2022-10-23 thomas *tag = got_repo_get_cached_tag(repo, id);
617 3efd8e31 2022-10-23 thomas if (*tag != NULL) {
618 3efd8e31 2022-10-23 thomas (*tag)->refcnt++;
619 3efd8e31 2022-10-23 thomas return NULL;
620 3efd8e31 2022-10-23 thomas }
621 3efd8e31 2022-10-23 thomas } else
622 3efd8e31 2022-10-23 thomas *tag = NULL;
623 3efd8e31 2022-10-23 thomas
624 3efd8e31 2022-10-23 thomas err = got_repo_search_packidx(&packidx, &idx, repo, id);
625 3efd8e31 2022-10-23 thomas if (err == NULL) {
626 3efd8e31 2022-10-23 thomas struct got_pack *pack = NULL;
627 3efd8e31 2022-10-23 thomas uint8_t *buf = NULL;
628 3efd8e31 2022-10-23 thomas size_t len;
629 3efd8e31 2022-10-23 thomas
630 3efd8e31 2022-10-23 thomas err = got_packidx_get_packfile_path(&path_packfile,
631 3efd8e31 2022-10-23 thomas packidx->path_packidx);
632 3efd8e31 2022-10-23 thomas if (err)
633 3efd8e31 2022-10-23 thomas return err;
634 3efd8e31 2022-10-23 thomas
635 3efd8e31 2022-10-23 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
636 3efd8e31 2022-10-23 thomas if (pack == NULL) {
637 3efd8e31 2022-10-23 thomas err = got_repo_cache_pack(&pack, repo, path_packfile,
638 3efd8e31 2022-10-23 thomas packidx);
639 3efd8e31 2022-10-23 thomas if (err)
640 3efd8e31 2022-10-23 thomas goto done;
641 3efd8e31 2022-10-23 thomas }
642 3efd8e31 2022-10-23 thomas
643 3efd8e31 2022-10-23 thomas /* Beware of "lightweight" tags: Check object type first. */
644 3efd8e31 2022-10-23 thomas err = got_packfile_open_object(&obj, pack, packidx, idx, id);
645 3efd8e31 2022-10-23 thomas if (err)
646 3efd8e31 2022-10-23 thomas goto done;
647 3efd8e31 2022-10-23 thomas obj_type = obj->type;
648 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_TAG) {
649 3efd8e31 2022-10-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
650 3efd8e31 2022-10-23 thomas got_object_close(obj);
651 3efd8e31 2022-10-23 thomas goto done;
652 3efd8e31 2022-10-23 thomas }
653 3efd8e31 2022-10-23 thomas err = got_packfile_extract_object_to_mem(&buf, &len,
654 3efd8e31 2022-10-23 thomas obj, pack);
655 3efd8e31 2022-10-23 thomas got_object_close(obj);
656 3efd8e31 2022-10-23 thomas if (err)
657 3efd8e31 2022-10-23 thomas goto done;
658 3efd8e31 2022-10-23 thomas err = got_object_parse_tag(tag, buf, len);
659 3efd8e31 2022-10-23 thomas free(buf);
660 3efd8e31 2022-10-23 thomas } else if (err->code == GOT_ERR_NO_OBJ) {
661 3efd8e31 2022-10-23 thomas int fd;
662 3efd8e31 2022-10-23 thomas
663 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
664 3efd8e31 2022-10-23 thomas if (err)
665 3efd8e31 2022-10-23 thomas return err;
666 3efd8e31 2022-10-23 thomas err = got_object_read_header(&obj, fd);
667 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
668 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
669 3efd8e31 2022-10-23 thomas if (err)
670 3efd8e31 2022-10-23 thomas return err;
671 3efd8e31 2022-10-23 thomas obj_type = obj->type;
672 3efd8e31 2022-10-23 thomas got_object_close(obj);
673 3efd8e31 2022-10-23 thomas if (obj_type != GOT_OBJ_TYPE_TAG)
674 3efd8e31 2022-10-23 thomas return got_error(GOT_ERR_OBJ_TYPE);
675 3efd8e31 2022-10-23 thomas
676 3efd8e31 2022-10-23 thomas err = got_object_open_loose_fd(&fd, id, repo);
677 3efd8e31 2022-10-23 thomas if (err)
678 3efd8e31 2022-10-23 thomas return err;
679 3efd8e31 2022-10-23 thomas err = got_object_read_tag(tag, fd, id, 0);
680 3efd8e31 2022-10-23 thomas if (close(fd) == -1 && err == NULL)
681 3efd8e31 2022-10-23 thomas err = got_error_from_errno("close");
682 3efd8e31 2022-10-23 thomas if (err)
683 3efd8e31 2022-10-23 thomas return err;
684 3efd8e31 2022-10-23 thomas }
685 3efd8e31 2022-10-23 thomas
686 3efd8e31 2022-10-23 thomas if (err == NULL) {
687 3efd8e31 2022-10-23 thomas (*tag)->refcnt++;
688 3efd8e31 2022-10-23 thomas err = got_repo_cache_tag(repo, id, *tag);
689 3efd8e31 2022-10-23 thomas if (err) {
690 3efd8e31 2022-10-23 thomas if (err->code == GOT_ERR_OBJ_EXISTS ||
691 3efd8e31 2022-10-23 thomas err->code == GOT_ERR_OBJ_TOO_LARGE)
692 3efd8e31 2022-10-23 thomas err = NULL;
693 3efd8e31 2022-10-23 thomas }
694 3efd8e31 2022-10-23 thomas }
695 3efd8e31 2022-10-23 thomas done:
696 3efd8e31 2022-10-23 thomas free(path_packfile);
697 3efd8e31 2022-10-23 thomas return err;
698 3efd8e31 2022-10-23 thomas }
699 3efd8e31 2022-10-23 thomas
700 3efd8e31 2022-10-23 thomas const struct got_error *
701 3efd8e31 2022-10-23 thomas got_object_open_as_tag(struct got_tag_object **tag,
702 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object_id *id)
703 3efd8e31 2022-10-23 thomas {
704 3efd8e31 2022-10-23 thomas *tag = got_repo_get_cached_tag(repo, id);
705 3efd8e31 2022-10-23 thomas if (*tag != NULL) {
706 3efd8e31 2022-10-23 thomas (*tag)->refcnt++;
707 3efd8e31 2022-10-23 thomas return NULL;
708 3efd8e31 2022-10-23 thomas }
709 3efd8e31 2022-10-23 thomas
710 3efd8e31 2022-10-23 thomas return open_tag(tag, repo, id, 0);
711 3efd8e31 2022-10-23 thomas }
712 3efd8e31 2022-10-23 thomas
713 3efd8e31 2022-10-23 thomas const struct got_error *
714 3efd8e31 2022-10-23 thomas got_object_tag_open(struct got_tag_object **tag,
715 3efd8e31 2022-10-23 thomas struct got_repository *repo, struct got_object *obj)
716 3efd8e31 2022-10-23 thomas {
717 3efd8e31 2022-10-23 thomas return open_tag(tag, repo, got_object_get_id(obj), 1);
718 3efd8e31 2022-10-23 thomas }