Blame


1 13b2bc37 2022-10-23 stsp /*
2 13b2bc37 2022-10-23 stsp * Copyright (c) 2022 Stefan Sperling <stsp@openbsd.org>
3 13b2bc37 2022-10-23 stsp *
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.
7 13b2bc37 2022-10-23 stsp *
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.
15 13b2bc37 2022-10-23 stsp */
16 13b2bc37 2022-10-23 stsp
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>
20 13b2bc37 2022-10-23 stsp
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>
29 13b2bc37 2022-10-23 stsp
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"
34 13b2bc37 2022-10-23 stsp
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"
41 13b2bc37 2022-10-23 stsp
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)
45 13b2bc37 2022-10-23 stsp {
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;
49 13b2bc37 2022-10-23 stsp int idx;
50 13b2bc37 2022-10-23 stsp char *path_packfile;
51 13b2bc37 2022-10-23 stsp
52 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
53 13b2bc37 2022-10-23 stsp if (err)
54 13b2bc37 2022-10-23 stsp return err;
55 13b2bc37 2022-10-23 stsp
56 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
57 13b2bc37 2022-10-23 stsp packidx->path_packidx);
58 13b2bc37 2022-10-23 stsp if (err)
59 13b2bc37 2022-10-23 stsp return err;
60 13b2bc37 2022-10-23 stsp
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);
64 13b2bc37 2022-10-23 stsp if (err)
65 13b2bc37 2022-10-23 stsp goto done;
66 13b2bc37 2022-10-23 stsp }
67 13b2bc37 2022-10-23 stsp
68 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
69 13b2bc37 2022-10-23 stsp if (err)
70 13b2bc37 2022-10-23 stsp return err;
71 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
72 13b2bc37 2022-10-23 stsp
73 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
74 13b2bc37 2022-10-23 stsp if (err) {
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)
77 13b2bc37 2022-10-23 stsp err = NULL;
78 13b2bc37 2022-10-23 stsp }
79 13b2bc37 2022-10-23 stsp done:
80 13b2bc37 2022-10-23 stsp free(path_packfile);
81 13b2bc37 2022-10-23 stsp return err;
82 13b2bc37 2022-10-23 stsp }
83 13b2bc37 2022-10-23 stsp
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)
88 13b2bc37 2022-10-23 stsp {
89 9afa3de2 2023-04-04 stsp const struct got_error *err;
90 9afa3de2 2023-04-04 stsp
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;
95 9afa3de2 2023-04-04 stsp }
96 9afa3de2 2023-04-04 stsp
97 9afa3de2 2023-04-04 stsp err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
98 9afa3de2 2023-04-04 stsp if (err)
99 9afa3de2 2023-04-04 stsp return err;
100 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
101 9afa3de2 2023-04-04 stsp
102 9afa3de2 2023-04-04 stsp err = got_repo_cache_object(repo, id, *obj);
103 9afa3de2 2023-04-04 stsp if (err) {
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;
108 9afa3de2 2023-04-04 stsp }
109 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
110 9afa3de2 2023-04-04 stsp return NULL;
111 13b2bc37 2022-10-23 stsp }
112 13b2bc37 2022-10-23 stsp
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)
119 13b2bc37 2022-10-23 stsp {
120 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
121 13b2bc37 2022-10-23 stsp }
122 13b2bc37 2022-10-23 stsp
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)
126 13b2bc37 2022-10-23 stsp {
127 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
128 13b2bc37 2022-10-23 stsp int fd;
129 13b2bc37 2022-10-23 stsp
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;
134 13b2bc37 2022-10-23 stsp }
135 13b2bc37 2022-10-23 stsp
136 13b2bc37 2022-10-23 stsp err = got_object_open_packed(obj, id, repo);
137 13b2bc37 2022-10-23 stsp if (err) {
138 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NO_OBJ)
139 13b2bc37 2022-10-23 stsp return err;
140 13b2bc37 2022-10-23 stsp } else
141 13b2bc37 2022-10-23 stsp return NULL;
142 13b2bc37 2022-10-23 stsp
143 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
144 13b2bc37 2022-10-23 stsp if (err) {
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;
148 13b2bc37 2022-10-23 stsp }
149 13b2bc37 2022-10-23 stsp
150 13b2bc37 2022-10-23 stsp err = got_object_read_header(obj, fd);
151 13b2bc37 2022-10-23 stsp if (err)
152 13b2bc37 2022-10-23 stsp goto done;
153 13b2bc37 2022-10-23 stsp
154 13b2bc37 2022-10-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
155 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
156 13b2bc37 2022-10-23 stsp
157 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
158 13b2bc37 2022-10-23 stsp if (err) {
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;
162 13b2bc37 2022-10-23 stsp }
163 13b2bc37 2022-10-23 stsp done:
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;
167 13b2bc37 2022-10-23 stsp }
168 13b2bc37 2022-10-23 stsp
169 13b2bc37 2022-10-23 stsp static const struct got_error *
170 13b2bc37 2022-10-23 stsp wrap_fd(FILE **f, int wrapped_fd)
171 13b2bc37 2022-10-23 stsp {
172 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
173 13b2bc37 2022-10-23 stsp int fd;
174 13b2bc37 2022-10-23 stsp
175 13b2bc37 2022-10-23 stsp if (ftruncate(wrapped_fd, 0L) == -1)
176 13b2bc37 2022-10-23 stsp return got_error_from_errno("ftruncate");
177 13b2bc37 2022-10-23 stsp
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");
180 13b2bc37 2022-10-23 stsp
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");
184 13b2bc37 2022-10-23 stsp
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");
188 13b2bc37 2022-10-23 stsp close(fd);
189 13b2bc37 2022-10-23 stsp }
190 13b2bc37 2022-10-23 stsp return err;
191 13b2bc37 2022-10-23 stsp }
192 13b2bc37 2022-10-23 stsp
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)
197 13b2bc37 2022-10-23 stsp {
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;
202 13b2bc37 2022-10-23 stsp
203 13b2bc37 2022-10-23 stsp *outbuf = NULL;
204 13b2bc37 2022-10-23 stsp *size = 0;
205 13b2bc37 2022-10-23 stsp *hdrlen = 0;
206 13b2bc37 2022-10-23 stsp
207 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
208 13b2bc37 2022-10-23 stsp if (err)
209 13b2bc37 2022-10-23 stsp return err;
210 13b2bc37 2022-10-23 stsp
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);
213 13b2bc37 2022-10-23 stsp if (err)
214 13b2bc37 2022-10-23 stsp goto done;
215 13b2bc37 2022-10-23 stsp } else
216 13b2bc37 2022-10-23 stsp raw_size = obj->size;
217 13b2bc37 2022-10-23 stsp
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);
222 13b2bc37 2022-10-23 stsp if (err)
223 13b2bc37 2022-10-23 stsp goto done;
224 13b2bc37 2022-10-23 stsp *size = (off_t)len;
225 13b2bc37 2022-10-23 stsp } else {
226 13b2bc37 2022-10-23 stsp /*
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().
229 13b2bc37 2022-10-23 stsp */
230 13b2bc37 2022-10-23 stsp err = wrap_fd(&outfile, outfd);
231 13b2bc37 2022-10-23 stsp if (err)
232 13b2bc37 2022-10-23 stsp goto done;
233 13b2bc37 2022-10-23 stsp err = wrap_fd(&basefile, pack->basefd);
234 13b2bc37 2022-10-23 stsp if (err)
235 13b2bc37 2022-10-23 stsp goto done;
236 13b2bc37 2022-10-23 stsp err = wrap_fd(&accumfile, pack->accumfd);
237 13b2bc37 2022-10-23 stsp if (err)
238 13b2bc37 2022-10-23 stsp goto done;
239 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
240 13b2bc37 2022-10-23 stsp accumfile);
241 13b2bc37 2022-10-23 stsp if (err)
242 13b2bc37 2022-10-23 stsp goto done;
243 95bdb85d 2023-01-09 stsp *size = obj->size;
244 13b2bc37 2022-10-23 stsp }
245 13b2bc37 2022-10-23 stsp
246 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
247 13b2bc37 2022-10-23 stsp done:
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;
256 13b2bc37 2022-10-23 stsp
257 13b2bc37 2022-10-23 stsp }
258 13b2bc37 2022-10-23 stsp
259 13b2bc37 2022-10-23 stsp static void
260 13b2bc37 2022-10-23 stsp put_raw_object_tempfile(struct got_raw_object *obj)
261 13b2bc37 2022-10-23 stsp {
262 13b2bc37 2022-10-23 stsp struct got_repository *repo = obj->close_arg;
263 13b2bc37 2022-10-23 stsp
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);
266 13b2bc37 2022-10-23 stsp }
267 13b2bc37 2022-10-23 stsp
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)
272 13b2bc37 2022-10-23 stsp {
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;
280 13b2bc37 2022-10-23 stsp
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;
285 13b2bc37 2022-10-23 stsp }
286 13b2bc37 2022-10-23 stsp
287 3a975a9f 2023-01-09 stsp err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
288 3a975a9f 2023-01-09 stsp if (err)
289 3a975a9f 2023-01-09 stsp return err;
290 13b2bc37 2022-10-23 stsp
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;
294 13b2bc37 2022-10-23 stsp
295 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
296 13b2bc37 2022-10-23 stsp packidx->path_packidx);
297 13b2bc37 2022-10-23 stsp if (err)
298 13b2bc37 2022-10-23 stsp goto done;
299 13b2bc37 2022-10-23 stsp
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,
303 13b2bc37 2022-10-23 stsp packidx);
304 13b2bc37 2022-10-23 stsp if (err)
305 13b2bc37 2022-10-23 stsp goto done;
306 13b2bc37 2022-10-23 stsp }
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);
309 13b2bc37 2022-10-23 stsp if (err)
310 13b2bc37 2022-10-23 stsp goto done;
311 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
312 13b2bc37 2022-10-23 stsp int fd;
313 13b2bc37 2022-10-23 stsp
314 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
315 13b2bc37 2022-10-23 stsp if (err)
316 13b2bc37 2022-10-23 stsp goto done;
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");
321 13b2bc37 2022-10-23 stsp if (err)
322 3a975a9f 2023-01-09 stsp goto done;
323 3a975a9f 2023-01-09 stsp }
324 3a975a9f 2023-01-09 stsp
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");
328 3a975a9f 2023-01-09 stsp goto done;
329 3a975a9f 2023-01-09 stsp }
330 3a975a9f 2023-01-09 stsp
331 3a975a9f 2023-01-09 stsp /*
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().
334 3a975a9f 2023-01-09 stsp */
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");
338 13b2bc37 2022-10-23 stsp goto done;
339 3a975a9f 2023-01-09 stsp }
340 13b2bc37 2022-10-23 stsp }
341 13b2bc37 2022-10-23 stsp
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);
344 13b2bc37 2022-10-23 stsp if (err)
345 13b2bc37 2022-10-23 stsp goto done;
346 13b2bc37 2022-10-23 stsp
347 13b2bc37 2022-10-23 stsp err = got_repo_cache_raw_object(repo, id, *obj);
348 13b2bc37 2022-10-23 stsp if (err) {
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;
352 13b2bc37 2022-10-23 stsp }
353 13b2bc37 2022-10-23 stsp done:
354 13b2bc37 2022-10-23 stsp free(path_packfile);
355 13b2bc37 2022-10-23 stsp if (err) {
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;
359 13b2bc37 2022-10-23 stsp }
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;
365 3a975a9f 2023-01-09 stsp }
366 13b2bc37 2022-10-23 stsp } else {
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;
373 3a975a9f 2023-01-09 stsp }
374 3a975a9f 2023-01-09 stsp } else {
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;
378 3a975a9f 2023-01-09 stsp }
379 13b2bc37 2022-10-23 stsp }
380 13b2bc37 2022-10-23 stsp return err;
381 13b2bc37 2022-10-23 stsp }
382 13b2bc37 2022-10-23 stsp
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)
386 13b2bc37 2022-10-23 stsp {
387 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
388 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
389 13b2bc37 2022-10-23 stsp int idx;
390 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
391 13b2bc37 2022-10-23 stsp
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;
397 13b2bc37 2022-10-23 stsp }
398 13b2bc37 2022-10-23 stsp } else
399 13b2bc37 2022-10-23 stsp *commit = NULL;
400 13b2bc37 2022-10-23 stsp
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;
407 13b2bc37 2022-10-23 stsp
408 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
409 13b2bc37 2022-10-23 stsp packidx->path_packidx);
410 13b2bc37 2022-10-23 stsp if (err)
411 13b2bc37 2022-10-23 stsp return err;
412 13b2bc37 2022-10-23 stsp
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,
416 13b2bc37 2022-10-23 stsp packidx);
417 13b2bc37 2022-10-23 stsp if (err)
418 13b2bc37 2022-10-23 stsp goto done;
419 13b2bc37 2022-10-23 stsp }
420 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
421 13b2bc37 2022-10-23 stsp if (err)
422 13b2bc37 2022-10-23 stsp goto done;
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);
426 13b2bc37 2022-10-23 stsp if (err)
427 13b2bc37 2022-10-23 stsp goto done;
428 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, buf, len);
429 13b2bc37 2022-10-23 stsp free(buf);
430 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
431 13b2bc37 2022-10-23 stsp int fd;
432 13b2bc37 2022-10-23 stsp
433 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
434 13b2bc37 2022-10-23 stsp if (err)
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");
439 13b2bc37 2022-10-23 stsp if (err)
440 13b2bc37 2022-10-23 stsp return err;
441 13b2bc37 2022-10-23 stsp }
442 13b2bc37 2022-10-23 stsp
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);
446 13b2bc37 2022-10-23 stsp if (err) {
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;
450 13b2bc37 2022-10-23 stsp }
451 13b2bc37 2022-10-23 stsp }
452 13b2bc37 2022-10-23 stsp done:
453 13b2bc37 2022-10-23 stsp free(path_packfile);
454 13b2bc37 2022-10-23 stsp return err;
455 13b2bc37 2022-10-23 stsp }
456 13b2bc37 2022-10-23 stsp
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)
460 13b2bc37 2022-10-23 stsp {
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;
465 13b2bc37 2022-10-23 stsp }
466 13b2bc37 2022-10-23 stsp
467 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, id, 0);
468 13b2bc37 2022-10-23 stsp }
469 13b2bc37 2022-10-23 stsp
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)
473 13b2bc37 2022-10-23 stsp {
474 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
475 13b2bc37 2022-10-23 stsp }
476 13b2bc37 2022-10-23 stsp
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)
480 13b2bc37 2022-10-23 stsp {
481 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
482 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
483 13b2bc37 2022-10-23 stsp int idx;
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;
488 13b2bc37 2022-10-23 stsp
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;
494 13b2bc37 2022-10-23 stsp }
495 13b2bc37 2022-10-23 stsp } else
496 13b2bc37 2022-10-23 stsp *tree = NULL;
497 13b2bc37 2022-10-23 stsp
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;
503 13b2bc37 2022-10-23 stsp
504 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
505 13b2bc37 2022-10-23 stsp packidx->path_packidx);
506 13b2bc37 2022-10-23 stsp if (err)
507 13b2bc37 2022-10-23 stsp return err;
508 13b2bc37 2022-10-23 stsp
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,
512 13b2bc37 2022-10-23 stsp packidx);
513 13b2bc37 2022-10-23 stsp if (err)
514 13b2bc37 2022-10-23 stsp goto done;
515 13b2bc37 2022-10-23 stsp }
516 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
517 13b2bc37 2022-10-23 stsp if (err)
518 13b2bc37 2022-10-23 stsp goto done;
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);
522 13b2bc37 2022-10-23 stsp if (err)
523 13b2bc37 2022-10-23 stsp goto done;
524 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(&entries, &nentries,
525 13b2bc37 2022-10-23 stsp &nentries_alloc, buf, len);
526 13b2bc37 2022-10-23 stsp if (err)
527 13b2bc37 2022-10-23 stsp goto done;
528 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
529 13b2bc37 2022-10-23 stsp int fd;
530 13b2bc37 2022-10-23 stsp
531 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
532 13b2bc37 2022-10-23 stsp if (err)
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");
538 13b2bc37 2022-10-23 stsp if (err)
539 13b2bc37 2022-10-23 stsp goto done;
540 13b2bc37 2022-10-23 stsp } else
541 13b2bc37 2022-10-23 stsp goto done;
542 13b2bc37 2022-10-23 stsp
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");
546 13b2bc37 2022-10-23 stsp goto done;
547 13b2bc37 2022-10-23 stsp }
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");
551 13b2bc37 2022-10-23 stsp goto done;
552 13b2bc37 2022-10-23 stsp }
553 13b2bc37 2022-10-23 stsp (*tree)->nentries = nentries;
554 13b2bc37 2022-10-23 stsp (*tree)->refcnt = 0;
555 13b2bc37 2022-10-23 stsp
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];
559 13b2bc37 2022-10-23 stsp
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);
563 13b2bc37 2022-10-23 stsp goto done;
564 13b2bc37 2022-10-23 stsp }
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;
568 13b2bc37 2022-10-23 stsp }
569 13b2bc37 2022-10-23 stsp done:
570 13b2bc37 2022-10-23 stsp free(path_packfile);
571 13b2bc37 2022-10-23 stsp free(entries);
572 13b2bc37 2022-10-23 stsp free(buf);
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);
576 13b2bc37 2022-10-23 stsp if (err) {
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;
580 13b2bc37 2022-10-23 stsp }
581 13b2bc37 2022-10-23 stsp }
582 13b2bc37 2022-10-23 stsp if (err) {
583 13b2bc37 2022-10-23 stsp if (*tree)
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;
587 13b2bc37 2022-10-23 stsp }
588 13b2bc37 2022-10-23 stsp return err;
589 13b2bc37 2022-10-23 stsp }
590 13b2bc37 2022-10-23 stsp
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)
594 13b2bc37 2022-10-23 stsp {
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;
599 13b2bc37 2022-10-23 stsp }
600 13b2bc37 2022-10-23 stsp
601 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, id, 0);
602 13b2bc37 2022-10-23 stsp }
603 13b2bc37 2022-10-23 stsp
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)
607 13b2bc37 2022-10-23 stsp {
608 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
609 13b2bc37 2022-10-23 stsp }
610 13b2bc37 2022-10-23 stsp
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,
614 13b2bc37 2022-10-23 stsp int outfd)
615 13b2bc37 2022-10-23 stsp {
616 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
617 13b2bc37 2022-10-23 stsp }
618 13b2bc37 2022-10-23 stsp
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,
622 13b2bc37 2022-10-23 stsp int outfd)
623 13b2bc37 2022-10-23 stsp {
624 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
625 13b2bc37 2022-10-23 stsp }
626 13b2bc37 2022-10-23 stsp
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)
630 13b2bc37 2022-10-23 stsp {
631 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
632 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
633 13b2bc37 2022-10-23 stsp int idx;
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;
637 13b2bc37 2022-10-23 stsp
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;
643 13b2bc37 2022-10-23 stsp }
644 13b2bc37 2022-10-23 stsp } else
645 13b2bc37 2022-10-23 stsp *tag = NULL;
646 13b2bc37 2022-10-23 stsp
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;
652 13b2bc37 2022-10-23 stsp
653 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
654 13b2bc37 2022-10-23 stsp packidx->path_packidx);
655 13b2bc37 2022-10-23 stsp if (err)
656 13b2bc37 2022-10-23 stsp return err;
657 13b2bc37 2022-10-23 stsp
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,
661 13b2bc37 2022-10-23 stsp packidx);
662 13b2bc37 2022-10-23 stsp if (err)
663 13b2bc37 2022-10-23 stsp goto done;
664 13b2bc37 2022-10-23 stsp }
665 13b2bc37 2022-10-23 stsp
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);
668 13b2bc37 2022-10-23 stsp if (err)
669 13b2bc37 2022-10-23 stsp goto done;
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);
674 13b2bc37 2022-10-23 stsp goto done;
675 13b2bc37 2022-10-23 stsp }
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);
679 13b2bc37 2022-10-23 stsp if (err)
680 13b2bc37 2022-10-23 stsp goto done;
681 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, buf, len);
682 13b2bc37 2022-10-23 stsp free(buf);
683 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
684 13b2bc37 2022-10-23 stsp int fd;
685 13b2bc37 2022-10-23 stsp
686 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
687 13b2bc37 2022-10-23 stsp if (err)
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");
692 13b2bc37 2022-10-23 stsp if (err)
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);
698 13b2bc37 2022-10-23 stsp
699 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
700 13b2bc37 2022-10-23 stsp if (err)
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");
705 13b2bc37 2022-10-23 stsp if (err)
706 13b2bc37 2022-10-23 stsp return err;
707 13b2bc37 2022-10-23 stsp }
708 13b2bc37 2022-10-23 stsp
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);
712 13b2bc37 2022-10-23 stsp if (err) {
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;
716 13b2bc37 2022-10-23 stsp }
717 13b2bc37 2022-10-23 stsp }
718 13b2bc37 2022-10-23 stsp done:
719 13b2bc37 2022-10-23 stsp free(path_packfile);
720 13b2bc37 2022-10-23 stsp return err;
721 13b2bc37 2022-10-23 stsp }
722 13b2bc37 2022-10-23 stsp
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)
726 13b2bc37 2022-10-23 stsp {
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;
731 13b2bc37 2022-10-23 stsp }
732 13b2bc37 2022-10-23 stsp
733 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, id, 0);
734 13b2bc37 2022-10-23 stsp }
735 13b2bc37 2022-10-23 stsp
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)
739 13b2bc37 2022-10-23 stsp {
740 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
741 13b2bc37 2022-10-23 stsp }