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 05fd3169 2024-03-19 stsp #include <zlib.h>
30 13b2bc37 2022-10-23 stsp
31 13b2bc37 2022-10-23 stsp #include "got_error.h"
32 13b2bc37 2022-10-23 stsp #include "got_object.h"
33 13b2bc37 2022-10-23 stsp #include "got_repository.h"
34 13b2bc37 2022-10-23 stsp #include "got_path.h"
35 13b2bc37 2022-10-23 stsp
36 13b2bc37 2022-10-23 stsp #include "got_lib_delta.h"
37 13b2bc37 2022-10-23 stsp #include "got_lib_object.h"
38 13b2bc37 2022-10-23 stsp #include "got_lib_object_cache.h"
39 13b2bc37 2022-10-23 stsp #include "got_lib_object_parse.h"
40 13b2bc37 2022-10-23 stsp #include "got_lib_pack.h"
41 13b2bc37 2022-10-23 stsp #include "got_lib_repository.h"
42 05fd3169 2024-03-19 stsp #include "got_lib_inflate.h"
43 05fd3169 2024-03-19 stsp #include "got_lib_hash.h"
44 13b2bc37 2022-10-23 stsp
45 13b2bc37 2022-10-23 stsp const struct got_error *
46 13b2bc37 2022-10-23 stsp got_object_open_packed(struct got_object **obj, struct got_object_id *id,
47 13b2bc37 2022-10-23 stsp struct got_repository *repo)
48 13b2bc37 2022-10-23 stsp {
49 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
50 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
51 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
52 13b2bc37 2022-10-23 stsp int idx;
53 13b2bc37 2022-10-23 stsp char *path_packfile;
54 13b2bc37 2022-10-23 stsp
55 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
56 13b2bc37 2022-10-23 stsp if (err)
57 13b2bc37 2022-10-23 stsp return err;
58 13b2bc37 2022-10-23 stsp
59 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
60 13b2bc37 2022-10-23 stsp packidx->path_packidx);
61 13b2bc37 2022-10-23 stsp if (err)
62 13b2bc37 2022-10-23 stsp return err;
63 13b2bc37 2022-10-23 stsp
64 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
65 13b2bc37 2022-10-23 stsp if (pack == NULL) {
66 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
67 13b2bc37 2022-10-23 stsp if (err)
68 13b2bc37 2022-10-23 stsp goto done;
69 13b2bc37 2022-10-23 stsp }
70 13b2bc37 2022-10-23 stsp
71 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
72 13b2bc37 2022-10-23 stsp if (err)
73 13b2bc37 2022-10-23 stsp return err;
74 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
75 13b2bc37 2022-10-23 stsp
76 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
77 13b2bc37 2022-10-23 stsp if (err) {
78 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
79 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
80 13b2bc37 2022-10-23 stsp err = NULL;
81 13b2bc37 2022-10-23 stsp }
82 13b2bc37 2022-10-23 stsp done:
83 13b2bc37 2022-10-23 stsp free(path_packfile);
84 13b2bc37 2022-10-23 stsp return err;
85 13b2bc37 2022-10-23 stsp }
86 13b2bc37 2022-10-23 stsp
87 13b2bc37 2022-10-23 stsp const struct got_error *
88 13b2bc37 2022-10-23 stsp got_object_open_from_packfile(struct got_object **obj, struct got_object_id *id,
89 13b2bc37 2022-10-23 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
90 13b2bc37 2022-10-23 stsp struct got_repository *repo)
91 13b2bc37 2022-10-23 stsp {
92 9afa3de2 2023-04-04 stsp const struct got_error *err;
93 9afa3de2 2023-04-04 stsp
94 9afa3de2 2023-04-04 stsp *obj = got_repo_get_cached_object(repo, id);
95 9afa3de2 2023-04-04 stsp if (*obj != NULL) {
96 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
97 9afa3de2 2023-04-04 stsp return NULL;
98 9afa3de2 2023-04-04 stsp }
99 9afa3de2 2023-04-04 stsp
100 9afa3de2 2023-04-04 stsp err = got_packfile_open_object(obj, pack, packidx, obj_idx, id);
101 9afa3de2 2023-04-04 stsp if (err)
102 9afa3de2 2023-04-04 stsp return err;
103 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
104 9afa3de2 2023-04-04 stsp
105 9afa3de2 2023-04-04 stsp err = got_repo_cache_object(repo, id, *obj);
106 9afa3de2 2023-04-04 stsp if (err) {
107 9afa3de2 2023-04-04 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
108 9afa3de2 2023-04-04 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
109 9afa3de2 2023-04-04 stsp err = NULL;
110 9afa3de2 2023-04-04 stsp return err;
111 9afa3de2 2023-04-04 stsp }
112 9afa3de2 2023-04-04 stsp (*obj)->refcnt++;
113 9afa3de2 2023-04-04 stsp return NULL;
114 13b2bc37 2022-10-23 stsp }
115 13b2bc37 2022-10-23 stsp
116 13b2bc37 2022-10-23 stsp const struct got_error *
117 13b2bc37 2022-10-23 stsp got_object_read_raw_delta(uint64_t *base_size, uint64_t *result_size,
118 13b2bc37 2022-10-23 stsp off_t *delta_size, off_t *delta_compressed_size, off_t *delta_offset,
119 13b2bc37 2022-10-23 stsp off_t *delta_out_offset, struct got_object_id **base_id, int delta_cache_fd,
120 13b2bc37 2022-10-23 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
121 13b2bc37 2022-10-23 stsp struct got_repository *repo)
122 13b2bc37 2022-10-23 stsp {
123 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_NOT_IMPL);
124 13b2bc37 2022-10-23 stsp }
125 13b2bc37 2022-10-23 stsp
126 13b2bc37 2022-10-23 stsp const struct got_error *
127 13b2bc37 2022-10-23 stsp got_object_open(struct got_object **obj, struct got_repository *repo,
128 13b2bc37 2022-10-23 stsp struct got_object_id *id)
129 13b2bc37 2022-10-23 stsp {
130 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
131 13b2bc37 2022-10-23 stsp int fd;
132 13b2bc37 2022-10-23 stsp
133 13b2bc37 2022-10-23 stsp *obj = got_repo_get_cached_object(repo, id);
134 13b2bc37 2022-10-23 stsp if (*obj != NULL) {
135 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
136 13b2bc37 2022-10-23 stsp return NULL;
137 13b2bc37 2022-10-23 stsp }
138 13b2bc37 2022-10-23 stsp
139 13b2bc37 2022-10-23 stsp err = got_object_open_packed(obj, id, repo);
140 13b2bc37 2022-10-23 stsp if (err) {
141 13b2bc37 2022-10-23 stsp if (err->code != GOT_ERR_NO_OBJ)
142 13b2bc37 2022-10-23 stsp return err;
143 13b2bc37 2022-10-23 stsp } else
144 13b2bc37 2022-10-23 stsp return NULL;
145 13b2bc37 2022-10-23 stsp
146 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
147 13b2bc37 2022-10-23 stsp if (err) {
148 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_ERRNO && errno == ENOENT)
149 13b2bc37 2022-10-23 stsp err = got_error_no_obj(id);
150 13b2bc37 2022-10-23 stsp return err;
151 13b2bc37 2022-10-23 stsp }
152 13b2bc37 2022-10-23 stsp
153 13b2bc37 2022-10-23 stsp err = got_object_read_header(obj, fd);
154 13b2bc37 2022-10-23 stsp if (err)
155 13b2bc37 2022-10-23 stsp goto done;
156 13b2bc37 2022-10-23 stsp
157 13b2bc37 2022-10-23 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
158 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
159 13b2bc37 2022-10-23 stsp
160 13b2bc37 2022-10-23 stsp err = got_repo_cache_object(repo, id, *obj);
161 13b2bc37 2022-10-23 stsp if (err) {
162 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
163 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
164 13b2bc37 2022-10-23 stsp err = NULL;
165 13b2bc37 2022-10-23 stsp }
166 13b2bc37 2022-10-23 stsp done:
167 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
168 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
169 13b2bc37 2022-10-23 stsp return err;
170 13b2bc37 2022-10-23 stsp }
171 13b2bc37 2022-10-23 stsp
172 13b2bc37 2022-10-23 stsp static const struct got_error *
173 13b2bc37 2022-10-23 stsp wrap_fd(FILE **f, int wrapped_fd)
174 13b2bc37 2022-10-23 stsp {
175 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
176 13b2bc37 2022-10-23 stsp int fd;
177 13b2bc37 2022-10-23 stsp
178 13b2bc37 2022-10-23 stsp if (ftruncate(wrapped_fd, 0L) == -1)
179 13b2bc37 2022-10-23 stsp return got_error_from_errno("ftruncate");
180 13b2bc37 2022-10-23 stsp
181 13b2bc37 2022-10-23 stsp if (lseek(wrapped_fd, 0L, SEEK_SET) == -1)
182 13b2bc37 2022-10-23 stsp return got_error_from_errno("lseek");
183 13b2bc37 2022-10-23 stsp
184 13b2bc37 2022-10-23 stsp fd = dup(wrapped_fd);
185 13b2bc37 2022-10-23 stsp if (fd == -1)
186 13b2bc37 2022-10-23 stsp return got_error_from_errno("dup");
187 13b2bc37 2022-10-23 stsp
188 13b2bc37 2022-10-23 stsp *f = fdopen(fd, "w+");
189 13b2bc37 2022-10-23 stsp if (*f == NULL) {
190 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fdopen");
191 13b2bc37 2022-10-23 stsp close(fd);
192 13b2bc37 2022-10-23 stsp }
193 13b2bc37 2022-10-23 stsp return err;
194 13b2bc37 2022-10-23 stsp }
195 13b2bc37 2022-10-23 stsp
196 13b2bc37 2022-10-23 stsp static const struct got_error *
197 13b2bc37 2022-10-23 stsp read_packed_object_raw(uint8_t **outbuf, off_t *size, size_t *hdrlen,
198 13b2bc37 2022-10-23 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
199 13b2bc37 2022-10-23 stsp struct got_object_id *id)
200 13b2bc37 2022-10-23 stsp {
201 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
202 13b2bc37 2022-10-23 stsp uint64_t raw_size = 0;
203 13b2bc37 2022-10-23 stsp struct got_object *obj;
204 13b2bc37 2022-10-23 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
205 13b2bc37 2022-10-23 stsp
206 13b2bc37 2022-10-23 stsp *outbuf = NULL;
207 13b2bc37 2022-10-23 stsp *size = 0;
208 13b2bc37 2022-10-23 stsp *hdrlen = 0;
209 13b2bc37 2022-10-23 stsp
210 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
211 13b2bc37 2022-10-23 stsp if (err)
212 13b2bc37 2022-10-23 stsp return err;
213 13b2bc37 2022-10-23 stsp
214 13b2bc37 2022-10-23 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
215 13b2bc37 2022-10-23 stsp err = got_pack_get_max_delta_object_size(&raw_size, obj, pack);
216 13b2bc37 2022-10-23 stsp if (err)
217 13b2bc37 2022-10-23 stsp goto done;
218 13b2bc37 2022-10-23 stsp } else
219 13b2bc37 2022-10-23 stsp raw_size = obj->size;
220 13b2bc37 2022-10-23 stsp
221 13b2bc37 2022-10-23 stsp if (raw_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
222 13b2bc37 2022-10-23 stsp size_t len;
223 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(outbuf, &len,
224 13b2bc37 2022-10-23 stsp obj, pack);
225 13b2bc37 2022-10-23 stsp if (err)
226 13b2bc37 2022-10-23 stsp goto done;
227 13b2bc37 2022-10-23 stsp *size = (off_t)len;
228 13b2bc37 2022-10-23 stsp } else {
229 13b2bc37 2022-10-23 stsp /*
230 13b2bc37 2022-10-23 stsp * XXX This uses 3 file extra descriptors for no good reason.
231 13b2bc37 2022-10-23 stsp * We should have got_packfile_extract_object_to_fd().
232 13b2bc37 2022-10-23 stsp */
233 13b2bc37 2022-10-23 stsp err = wrap_fd(&outfile, outfd);
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(&basefile, pack->basefd);
237 13b2bc37 2022-10-23 stsp if (err)
238 13b2bc37 2022-10-23 stsp goto done;
239 13b2bc37 2022-10-23 stsp err = wrap_fd(&accumfile, pack->accumfd);
240 13b2bc37 2022-10-23 stsp if (err)
241 13b2bc37 2022-10-23 stsp goto done;
242 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
243 13b2bc37 2022-10-23 stsp accumfile);
244 13b2bc37 2022-10-23 stsp if (err)
245 13b2bc37 2022-10-23 stsp goto done;
246 95bdb85d 2023-01-09 stsp *size = obj->size;
247 13b2bc37 2022-10-23 stsp }
248 13b2bc37 2022-10-23 stsp
249 13b2bc37 2022-10-23 stsp *hdrlen = obj->hdrlen;
250 13b2bc37 2022-10-23 stsp done:
251 13b2bc37 2022-10-23 stsp got_object_close(obj);
252 13b2bc37 2022-10-23 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
253 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
254 13b2bc37 2022-10-23 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
255 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
256 13b2bc37 2022-10-23 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
257 13b2bc37 2022-10-23 stsp err = got_error_from_errno("fclose");
258 13b2bc37 2022-10-23 stsp return err;
259 13b2bc37 2022-10-23 stsp
260 13b2bc37 2022-10-23 stsp }
261 13b2bc37 2022-10-23 stsp
262 13b2bc37 2022-10-23 stsp static void
263 13b2bc37 2022-10-23 stsp put_raw_object_tempfile(struct got_raw_object *obj)
264 13b2bc37 2022-10-23 stsp {
265 13b2bc37 2022-10-23 stsp struct got_repository *repo = obj->close_arg;
266 13b2bc37 2022-10-23 stsp
267 13b2bc37 2022-10-23 stsp if (obj->tempfile_idx != -1)
268 13b2bc37 2022-10-23 stsp got_repo_temp_fds_put(obj->tempfile_idx, repo);
269 13b2bc37 2022-10-23 stsp }
270 13b2bc37 2022-10-23 stsp
271 13b2bc37 2022-10-23 stsp /* *outfd must be initialized to -1 by caller */
272 13b2bc37 2022-10-23 stsp const struct got_error *
273 13b2bc37 2022-10-23 stsp got_object_raw_open(struct got_raw_object **obj, int *outfd,
274 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
275 13b2bc37 2022-10-23 stsp {
276 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
277 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
278 3a975a9f 2023-01-09 stsp int idx, tempfd, tempfile_idx;
279 13b2bc37 2022-10-23 stsp uint8_t *outbuf = NULL;
280 13b2bc37 2022-10-23 stsp off_t size = 0;
281 13b2bc37 2022-10-23 stsp size_t hdrlen = 0;
282 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
283 13b2bc37 2022-10-23 stsp
284 13b2bc37 2022-10-23 stsp *obj = got_repo_get_cached_raw_object(repo, id);
285 13b2bc37 2022-10-23 stsp if (*obj != NULL) {
286 13b2bc37 2022-10-23 stsp (*obj)->refcnt++;
287 13b2bc37 2022-10-23 stsp return NULL;
288 13b2bc37 2022-10-23 stsp }
289 13b2bc37 2022-10-23 stsp
290 3a975a9f 2023-01-09 stsp err = got_repo_temp_fds_get(&tempfd, &tempfile_idx, repo);
291 3a975a9f 2023-01-09 stsp if (err)
292 3a975a9f 2023-01-09 stsp return err;
293 13b2bc37 2022-10-23 stsp
294 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
295 13b2bc37 2022-10-23 stsp if (err == NULL) {
296 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
297 13b2bc37 2022-10-23 stsp
298 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
299 13b2bc37 2022-10-23 stsp packidx->path_packidx);
300 13b2bc37 2022-10-23 stsp if (err)
301 13b2bc37 2022-10-23 stsp goto done;
302 13b2bc37 2022-10-23 stsp
303 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
304 13b2bc37 2022-10-23 stsp if (pack == NULL) {
305 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
306 13b2bc37 2022-10-23 stsp packidx);
307 13b2bc37 2022-10-23 stsp if (err)
308 13b2bc37 2022-10-23 stsp goto done;
309 13b2bc37 2022-10-23 stsp }
310 13b2bc37 2022-10-23 stsp err = read_packed_object_raw(&outbuf, &size, &hdrlen,
311 3a975a9f 2023-01-09 stsp tempfd, pack, packidx, idx, id);
312 13b2bc37 2022-10-23 stsp if (err)
313 13b2bc37 2022-10-23 stsp goto done;
314 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
315 13b2bc37 2022-10-23 stsp int fd;
316 13b2bc37 2022-10-23 stsp
317 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
318 13b2bc37 2022-10-23 stsp if (err)
319 13b2bc37 2022-10-23 stsp goto done;
320 13b2bc37 2022-10-23 stsp err = got_object_read_raw(&outbuf, &size, &hdrlen,
321 3a975a9f 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, tempfd, id, fd);
322 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
323 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
324 13b2bc37 2022-10-23 stsp if (err)
325 3a975a9f 2023-01-09 stsp goto done;
326 3a975a9f 2023-01-09 stsp }
327 3a975a9f 2023-01-09 stsp
328 3a975a9f 2023-01-09 stsp if (outbuf == NULL) {
329 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
330 3a975a9f 2023-01-09 stsp err = got_error_msg(GOT_ERR_NOT_IMPL, "bad outfd");
331 3a975a9f 2023-01-09 stsp goto done;
332 3a975a9f 2023-01-09 stsp }
333 3a975a9f 2023-01-09 stsp
334 3a975a9f 2023-01-09 stsp /*
335 3a975a9f 2023-01-09 stsp * Duplicate tempfile descriptor to allow use of
336 3a975a9f 2023-01-09 stsp * fdopen(3) inside got_object_raw_alloc().
337 3a975a9f 2023-01-09 stsp */
338 3a975a9f 2023-01-09 stsp *outfd = dup(tempfd);
339 3a975a9f 2023-01-09 stsp if (*outfd == -1) {
340 3a975a9f 2023-01-09 stsp err = got_error_from_errno("dup");
341 13b2bc37 2022-10-23 stsp goto done;
342 3a975a9f 2023-01-09 stsp }
343 13b2bc37 2022-10-23 stsp }
344 13b2bc37 2022-10-23 stsp
345 60c140ae 2023-01-09 stsp err = got_object_raw_alloc(obj, outbuf, outfd,
346 60c140ae 2023-01-09 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX, hdrlen, size);
347 13b2bc37 2022-10-23 stsp if (err)
348 13b2bc37 2022-10-23 stsp goto done;
349 13b2bc37 2022-10-23 stsp
350 13b2bc37 2022-10-23 stsp err = got_repo_cache_raw_object(repo, id, *obj);
351 13b2bc37 2022-10-23 stsp if (err) {
352 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
353 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
354 13b2bc37 2022-10-23 stsp err = NULL;
355 13b2bc37 2022-10-23 stsp }
356 13b2bc37 2022-10-23 stsp done:
357 13b2bc37 2022-10-23 stsp free(path_packfile);
358 13b2bc37 2022-10-23 stsp if (err) {
359 13b2bc37 2022-10-23 stsp if (*obj) {
360 13b2bc37 2022-10-23 stsp got_object_raw_close(*obj);
361 13b2bc37 2022-10-23 stsp *obj = NULL;
362 13b2bc37 2022-10-23 stsp }
363 13b2bc37 2022-10-23 stsp free(outbuf);
364 3a975a9f 2023-01-09 stsp got_repo_temp_fds_put(tempfile_idx, repo);
365 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
366 3a975a9f 2023-01-09 stsp close(*outfd);
367 3a975a9f 2023-01-09 stsp *outfd = -1;
368 3a975a9f 2023-01-09 stsp }
369 13b2bc37 2022-10-23 stsp } else {
370 3a975a9f 2023-01-09 stsp if (((*obj)->f == NULL && (*obj)->fd == -1)) {
371 3a975a9f 2023-01-09 stsp /* This raw object is not backed by a file. */
372 3a975a9f 2023-01-09 stsp got_repo_temp_fds_put(tempfile_idx, repo);
373 3a975a9f 2023-01-09 stsp if (*outfd != -1) {
374 3a975a9f 2023-01-09 stsp close(*outfd);
375 3a975a9f 2023-01-09 stsp *outfd = -1;
376 3a975a9f 2023-01-09 stsp }
377 3a975a9f 2023-01-09 stsp } else {
378 3a975a9f 2023-01-09 stsp (*obj)->tempfile_idx = tempfile_idx;
379 3a975a9f 2023-01-09 stsp (*obj)->close_cb = put_raw_object_tempfile;
380 3a975a9f 2023-01-09 stsp (*obj)->close_arg = repo;
381 3a975a9f 2023-01-09 stsp }
382 13b2bc37 2022-10-23 stsp }
383 13b2bc37 2022-10-23 stsp return err;
384 13b2bc37 2022-10-23 stsp }
385 13b2bc37 2022-10-23 stsp
386 13b2bc37 2022-10-23 stsp static const struct got_error *
387 13b2bc37 2022-10-23 stsp open_commit(struct got_commit_object **commit,
388 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
389 13b2bc37 2022-10-23 stsp {
390 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
391 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
392 13b2bc37 2022-10-23 stsp int idx;
393 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
394 13b2bc37 2022-10-23 stsp
395 13b2bc37 2022-10-23 stsp if (check_cache) {
396 13b2bc37 2022-10-23 stsp *commit = got_repo_get_cached_commit(repo, id);
397 13b2bc37 2022-10-23 stsp if (*commit != NULL) {
398 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
399 13b2bc37 2022-10-23 stsp return NULL;
400 13b2bc37 2022-10-23 stsp }
401 13b2bc37 2022-10-23 stsp } else
402 13b2bc37 2022-10-23 stsp *commit = NULL;
403 13b2bc37 2022-10-23 stsp
404 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
405 13b2bc37 2022-10-23 stsp if (err == NULL) {
406 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
407 13b2bc37 2022-10-23 stsp struct got_object *obj;
408 13b2bc37 2022-10-23 stsp uint8_t *buf;
409 13b2bc37 2022-10-23 stsp size_t len;
410 13b2bc37 2022-10-23 stsp
411 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
412 13b2bc37 2022-10-23 stsp packidx->path_packidx);
413 13b2bc37 2022-10-23 stsp if (err)
414 13b2bc37 2022-10-23 stsp return err;
415 13b2bc37 2022-10-23 stsp
416 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
417 13b2bc37 2022-10-23 stsp if (pack == NULL) {
418 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
419 13b2bc37 2022-10-23 stsp packidx);
420 13b2bc37 2022-10-23 stsp if (err)
421 13b2bc37 2022-10-23 stsp goto done;
422 13b2bc37 2022-10-23 stsp }
423 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
424 13b2bc37 2022-10-23 stsp if (err)
425 13b2bc37 2022-10-23 stsp goto done;
426 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
427 13b2bc37 2022-10-23 stsp obj, pack);
428 13b2bc37 2022-10-23 stsp got_object_close(obj);
429 13b2bc37 2022-10-23 stsp if (err)
430 13b2bc37 2022-10-23 stsp goto done;
431 13b2bc37 2022-10-23 stsp err = got_object_parse_commit(commit, buf, len);
432 13b2bc37 2022-10-23 stsp free(buf);
433 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
434 13b2bc37 2022-10-23 stsp int fd;
435 13b2bc37 2022-10-23 stsp
436 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
437 13b2bc37 2022-10-23 stsp if (err)
438 13b2bc37 2022-10-23 stsp return err;
439 13b2bc37 2022-10-23 stsp err = got_object_read_commit(commit, fd, id, 0);
440 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
441 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
442 13b2bc37 2022-10-23 stsp if (err)
443 13b2bc37 2022-10-23 stsp return err;
444 13b2bc37 2022-10-23 stsp }
445 13b2bc37 2022-10-23 stsp
446 13b2bc37 2022-10-23 stsp if (err == NULL) {
447 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
448 13b2bc37 2022-10-23 stsp err = got_repo_cache_commit(repo, id, *commit);
449 13b2bc37 2022-10-23 stsp if (err) {
450 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
451 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
452 13b2bc37 2022-10-23 stsp err = NULL;
453 13b2bc37 2022-10-23 stsp }
454 13b2bc37 2022-10-23 stsp }
455 13b2bc37 2022-10-23 stsp done:
456 13b2bc37 2022-10-23 stsp free(path_packfile);
457 13b2bc37 2022-10-23 stsp return err;
458 13b2bc37 2022-10-23 stsp }
459 13b2bc37 2022-10-23 stsp
460 13b2bc37 2022-10-23 stsp const struct got_error *
461 13b2bc37 2022-10-23 stsp got_object_open_as_commit(struct got_commit_object **commit,
462 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
463 13b2bc37 2022-10-23 stsp {
464 13b2bc37 2022-10-23 stsp *commit = got_repo_get_cached_commit(repo, id);
465 13b2bc37 2022-10-23 stsp if (*commit != NULL) {
466 13b2bc37 2022-10-23 stsp (*commit)->refcnt++;
467 13b2bc37 2022-10-23 stsp return NULL;
468 13b2bc37 2022-10-23 stsp }
469 13b2bc37 2022-10-23 stsp
470 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, id, 0);
471 13b2bc37 2022-10-23 stsp }
472 13b2bc37 2022-10-23 stsp
473 13b2bc37 2022-10-23 stsp const struct got_error *
474 13b2bc37 2022-10-23 stsp got_object_commit_open(struct got_commit_object **commit,
475 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
476 13b2bc37 2022-10-23 stsp {
477 13b2bc37 2022-10-23 stsp return open_commit(commit, repo, got_object_get_id(obj), 1);
478 13b2bc37 2022-10-23 stsp }
479 13b2bc37 2022-10-23 stsp
480 13b2bc37 2022-10-23 stsp static const struct got_error *
481 13b2bc37 2022-10-23 stsp open_tree(struct got_tree_object **tree,
482 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, int check_cache)
483 13b2bc37 2022-10-23 stsp {
484 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
485 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
486 13b2bc37 2022-10-23 stsp int idx;
487 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
488 13b2bc37 2022-10-23 stsp struct got_parsed_tree_entry *entries = NULL;
489 13b2bc37 2022-10-23 stsp size_t nentries = 0, nentries_alloc = 0, i;
490 13b2bc37 2022-10-23 stsp uint8_t *buf = NULL;
491 13b2bc37 2022-10-23 stsp
492 13b2bc37 2022-10-23 stsp if (check_cache) {
493 13b2bc37 2022-10-23 stsp *tree = got_repo_get_cached_tree(repo, id);
494 13b2bc37 2022-10-23 stsp if (*tree != NULL) {
495 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
496 13b2bc37 2022-10-23 stsp return NULL;
497 13b2bc37 2022-10-23 stsp }
498 13b2bc37 2022-10-23 stsp } else
499 13b2bc37 2022-10-23 stsp *tree = NULL;
500 13b2bc37 2022-10-23 stsp
501 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
502 13b2bc37 2022-10-23 stsp if (err == NULL) {
503 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
504 13b2bc37 2022-10-23 stsp struct got_object *obj;
505 13b2bc37 2022-10-23 stsp size_t len;
506 13b2bc37 2022-10-23 stsp
507 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
508 13b2bc37 2022-10-23 stsp packidx->path_packidx);
509 13b2bc37 2022-10-23 stsp if (err)
510 13b2bc37 2022-10-23 stsp return err;
511 13b2bc37 2022-10-23 stsp
512 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
513 13b2bc37 2022-10-23 stsp if (pack == NULL) {
514 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
515 13b2bc37 2022-10-23 stsp packidx);
516 13b2bc37 2022-10-23 stsp if (err)
517 13b2bc37 2022-10-23 stsp goto done;
518 13b2bc37 2022-10-23 stsp }
519 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
520 13b2bc37 2022-10-23 stsp if (err)
521 13b2bc37 2022-10-23 stsp goto done;
522 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
523 13b2bc37 2022-10-23 stsp obj, pack);
524 13b2bc37 2022-10-23 stsp got_object_close(obj);
525 13b2bc37 2022-10-23 stsp if (err)
526 13b2bc37 2022-10-23 stsp goto done;
527 13b2bc37 2022-10-23 stsp err = got_object_parse_tree(&entries, &nentries,
528 13b2bc37 2022-10-23 stsp &nentries_alloc, buf, len);
529 13b2bc37 2022-10-23 stsp if (err)
530 13b2bc37 2022-10-23 stsp goto done;
531 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
532 13b2bc37 2022-10-23 stsp int fd;
533 13b2bc37 2022-10-23 stsp
534 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
535 13b2bc37 2022-10-23 stsp if (err)
536 13b2bc37 2022-10-23 stsp return err;
537 13b2bc37 2022-10-23 stsp err = got_object_read_tree(&entries, &nentries,
538 13b2bc37 2022-10-23 stsp &nentries_alloc, &buf, fd, id);
539 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
540 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
541 13b2bc37 2022-10-23 stsp if (err)
542 13b2bc37 2022-10-23 stsp goto done;
543 13b2bc37 2022-10-23 stsp } else
544 13b2bc37 2022-10-23 stsp goto done;
545 13b2bc37 2022-10-23 stsp
546 13b2bc37 2022-10-23 stsp *tree = malloc(sizeof(**tree));
547 13b2bc37 2022-10-23 stsp if (*tree == NULL) {
548 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
549 13b2bc37 2022-10-23 stsp goto done;
550 13b2bc37 2022-10-23 stsp }
551 13b2bc37 2022-10-23 stsp (*tree)->entries = calloc(nentries, sizeof(struct got_tree_entry));
552 13b2bc37 2022-10-23 stsp if ((*tree)->entries == NULL) {
553 13b2bc37 2022-10-23 stsp err = got_error_from_errno("malloc");
554 13b2bc37 2022-10-23 stsp goto done;
555 13b2bc37 2022-10-23 stsp }
556 13b2bc37 2022-10-23 stsp (*tree)->nentries = nentries;
557 13b2bc37 2022-10-23 stsp (*tree)->refcnt = 0;
558 13b2bc37 2022-10-23 stsp
559 13b2bc37 2022-10-23 stsp for (i = 0; i < nentries; i++) {
560 13b2bc37 2022-10-23 stsp struct got_parsed_tree_entry *pe = &entries[i];
561 13b2bc37 2022-10-23 stsp struct got_tree_entry *te = &(*tree)->entries[i];
562 13b2bc37 2022-10-23 stsp
563 13b2bc37 2022-10-23 stsp if (strlcpy(te->name, pe->name,
564 13b2bc37 2022-10-23 stsp sizeof(te->name)) >= sizeof(te->name)) {
565 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_NO_SPACE);
566 13b2bc37 2022-10-23 stsp goto done;
567 13b2bc37 2022-10-23 stsp }
568 13b2bc37 2022-10-23 stsp memcpy(te->id.sha1, pe->id, SHA1_DIGEST_LENGTH);
569 13b2bc37 2022-10-23 stsp te->mode = pe->mode;
570 13b2bc37 2022-10-23 stsp te->idx = i;
571 13b2bc37 2022-10-23 stsp }
572 13b2bc37 2022-10-23 stsp done:
573 13b2bc37 2022-10-23 stsp free(path_packfile);
574 13b2bc37 2022-10-23 stsp free(entries);
575 13b2bc37 2022-10-23 stsp free(buf);
576 13b2bc37 2022-10-23 stsp if (err == NULL) {
577 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
578 13b2bc37 2022-10-23 stsp err = got_repo_cache_tree(repo, id, *tree);
579 13b2bc37 2022-10-23 stsp if (err) {
580 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
581 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
582 13b2bc37 2022-10-23 stsp err = NULL;
583 13b2bc37 2022-10-23 stsp }
584 13b2bc37 2022-10-23 stsp }
585 13b2bc37 2022-10-23 stsp if (err) {
586 13b2bc37 2022-10-23 stsp if (*tree)
587 13b2bc37 2022-10-23 stsp free((*tree)->entries);
588 13b2bc37 2022-10-23 stsp free(*tree);
589 13b2bc37 2022-10-23 stsp *tree = NULL;
590 13b2bc37 2022-10-23 stsp }
591 13b2bc37 2022-10-23 stsp return err;
592 13b2bc37 2022-10-23 stsp }
593 13b2bc37 2022-10-23 stsp
594 13b2bc37 2022-10-23 stsp const struct got_error *
595 13b2bc37 2022-10-23 stsp got_object_open_as_tree(struct got_tree_object **tree,
596 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
597 13b2bc37 2022-10-23 stsp {
598 13b2bc37 2022-10-23 stsp *tree = got_repo_get_cached_tree(repo, id);
599 13b2bc37 2022-10-23 stsp if (*tree != NULL) {
600 13b2bc37 2022-10-23 stsp (*tree)->refcnt++;
601 13b2bc37 2022-10-23 stsp return NULL;
602 13b2bc37 2022-10-23 stsp }
603 13b2bc37 2022-10-23 stsp
604 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, id, 0);
605 13b2bc37 2022-10-23 stsp }
606 13b2bc37 2022-10-23 stsp
607 13b2bc37 2022-10-23 stsp const struct got_error *
608 13b2bc37 2022-10-23 stsp got_object_tree_open(struct got_tree_object **tree,
609 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
610 13b2bc37 2022-10-23 stsp {
611 13b2bc37 2022-10-23 stsp return open_tree(tree, repo, got_object_get_id(obj), 1);
612 13b2bc37 2022-10-23 stsp }
613 05fd3169 2024-03-19 stsp
614 05fd3169 2024-03-19 stsp static const struct got_error *
615 05fd3169 2024-03-19 stsp read_packed_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen,
616 05fd3169 2024-03-19 stsp int outfd, struct got_pack *pack, struct got_packidx *packidx, int idx,
617 05fd3169 2024-03-19 stsp struct got_object_id *id, struct got_repository *repo)
618 05fd3169 2024-03-19 stsp {
619 05fd3169 2024-03-19 stsp const struct got_error *err = NULL;
620 05fd3169 2024-03-19 stsp struct got_object *obj;
621 05fd3169 2024-03-19 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
622 05fd3169 2024-03-19 stsp uint64_t blob_size;
623 05fd3169 2024-03-19 stsp
624 05fd3169 2024-03-19 stsp *hdrlen = 0;
625 05fd3169 2024-03-19 stsp
626 05fd3169 2024-03-19 stsp err = got_object_open_from_packfile(&obj, id, pack, packidx, idx,
627 05fd3169 2024-03-19 stsp repo);
628 05fd3169 2024-03-19 stsp if (err)
629 05fd3169 2024-03-19 stsp return err;
630 13b2bc37 2022-10-23 stsp
631 05fd3169 2024-03-19 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
632 05fd3169 2024-03-19 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj,
633 05fd3169 2024-03-19 stsp pack);
634 05fd3169 2024-03-19 stsp if (err)
635 05fd3169 2024-03-19 stsp goto done;
636 05fd3169 2024-03-19 stsp } else
637 05fd3169 2024-03-19 stsp blob_size = obj->size;
638 05fd3169 2024-03-19 stsp
639 05fd3169 2024-03-19 stsp if (blob_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
640 05fd3169 2024-03-19 stsp err = got_packfile_extract_object_to_mem(outbuf, size,
641 05fd3169 2024-03-19 stsp obj, pack);
642 05fd3169 2024-03-19 stsp } else {
643 05fd3169 2024-03-19 stsp /*
644 05fd3169 2024-03-19 stsp * XXX This uses 3 file extra descriptors for no good reason.
645 05fd3169 2024-03-19 stsp * We should have got_packfile_extract_object_to_fd().
646 05fd3169 2024-03-19 stsp */
647 05fd3169 2024-03-19 stsp err = wrap_fd(&outfile, outfd);
648 05fd3169 2024-03-19 stsp if (err)
649 05fd3169 2024-03-19 stsp goto done;
650 05fd3169 2024-03-19 stsp err = wrap_fd(&basefile, pack->basefd);
651 05fd3169 2024-03-19 stsp if (err)
652 05fd3169 2024-03-19 stsp goto done;
653 05fd3169 2024-03-19 stsp err = wrap_fd(&accumfile, pack->accumfd);
654 05fd3169 2024-03-19 stsp if (err)
655 05fd3169 2024-03-19 stsp goto done;
656 05fd3169 2024-03-19 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
657 05fd3169 2024-03-19 stsp accumfile);
658 05fd3169 2024-03-19 stsp if (err)
659 05fd3169 2024-03-19 stsp goto done;
660 05fd3169 2024-03-19 stsp *size = obj->size;
661 05fd3169 2024-03-19 stsp }
662 05fd3169 2024-03-19 stsp
663 05fd3169 2024-03-19 stsp /* XXX verify checksum? */
664 05fd3169 2024-03-19 stsp done:
665 05fd3169 2024-03-19 stsp got_object_close(obj);
666 05fd3169 2024-03-19 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
667 05fd3169 2024-03-19 stsp err = got_error_from_errno("fclose");
668 05fd3169 2024-03-19 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
669 05fd3169 2024-03-19 stsp err = got_error_from_errno("fclose");
670 05fd3169 2024-03-19 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
671 05fd3169 2024-03-19 stsp err = got_error_from_errno("fclose");
672 05fd3169 2024-03-19 stsp return err;
673 05fd3169 2024-03-19 stsp }
674 05fd3169 2024-03-19 stsp
675 05fd3169 2024-03-19 stsp static const struct got_error *
676 05fd3169 2024-03-19 stsp read_blob(uint8_t **outbuf, size_t *size, size_t *hdrlen, int outfd, int infd,
677 05fd3169 2024-03-19 stsp struct got_object_id *id, struct got_repository *repo)
678 05fd3169 2024-03-19 stsp {
679 05fd3169 2024-03-19 stsp const struct got_error *err = NULL;
680 05fd3169 2024-03-19 stsp struct got_object *obj = NULL;
681 05fd3169 2024-03-19 stsp FILE *f = NULL;
682 05fd3169 2024-03-19 stsp struct got_object_id expected_id;
683 05fd3169 2024-03-19 stsp struct got_inflate_checksum csum;
684 05fd3169 2024-03-19 stsp struct got_hash ctx;
685 05fd3169 2024-03-19 stsp
686 05fd3169 2024-03-19 stsp got_hash_init(&ctx, GOT_HASH_SHA1);
687 05fd3169 2024-03-19 stsp memset(&csum, 0, sizeof(csum));
688 05fd3169 2024-03-19 stsp csum.output_ctx = &ctx;
689 05fd3169 2024-03-19 stsp
690 05fd3169 2024-03-19 stsp memcpy(&expected_id, id, sizeof(expected_id));
691 05fd3169 2024-03-19 stsp
692 05fd3169 2024-03-19 stsp err = got_object_read_header(&obj, infd);
693 05fd3169 2024-03-19 stsp if (err)
694 05fd3169 2024-03-19 stsp goto done;
695 05fd3169 2024-03-19 stsp
696 05fd3169 2024-03-19 stsp if (lseek(infd, SEEK_SET, 0) == -1) {
697 05fd3169 2024-03-19 stsp err = got_error_from_errno("lseek");
698 05fd3169 2024-03-19 stsp goto done;
699 05fd3169 2024-03-19 stsp }
700 05fd3169 2024-03-19 stsp
701 05fd3169 2024-03-19 stsp f = fdopen(infd, "rb");
702 05fd3169 2024-03-19 stsp if (f == NULL) {
703 05fd3169 2024-03-19 stsp err = got_error_from_errno("fdopen");
704 05fd3169 2024-03-19 stsp goto done;
705 05fd3169 2024-03-19 stsp }
706 05fd3169 2024-03-19 stsp infd = -1;
707 05fd3169 2024-03-19 stsp
708 05fd3169 2024-03-19 stsp if (obj->size + obj->hdrlen <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
709 05fd3169 2024-03-19 stsp err = got_inflate_to_mem(outbuf, size, NULL, &csum, f);
710 05fd3169 2024-03-19 stsp if (err)
711 05fd3169 2024-03-19 stsp goto done;
712 05fd3169 2024-03-19 stsp } else {
713 05fd3169 2024-03-19 stsp err = got_inflate_to_fd(size, f, &csum, outfd);
714 05fd3169 2024-03-19 stsp if (err)
715 05fd3169 2024-03-19 stsp goto done;
716 05fd3169 2024-03-19 stsp }
717 05fd3169 2024-03-19 stsp
718 05fd3169 2024-03-19 stsp if (*size < obj->hdrlen) {
719 05fd3169 2024-03-19 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
720 05fd3169 2024-03-19 stsp goto done;
721 05fd3169 2024-03-19 stsp }
722 05fd3169 2024-03-19 stsp
723 05fd3169 2024-03-19 stsp *hdrlen = obj->hdrlen;
724 05fd3169 2024-03-19 stsp
725 05fd3169 2024-03-19 stsp got_hash_final_object_id(&ctx, id);
726 05fd3169 2024-03-19 stsp if (got_object_id_cmp(&expected_id, id) != 0) {
727 05fd3169 2024-03-19 stsp err = got_error_checksum(&expected_id);
728 05fd3169 2024-03-19 stsp goto done;
729 05fd3169 2024-03-19 stsp }
730 05fd3169 2024-03-19 stsp done:
731 05fd3169 2024-03-19 stsp if (f && fclose(f) == EOF && err == NULL)
732 05fd3169 2024-03-19 stsp err = got_error_from_errno("fclose");
733 05fd3169 2024-03-19 stsp if (infd != -1 && close(infd) == -1 && err == NULL)
734 05fd3169 2024-03-19 stsp err = got_error_from_errno("close");
735 05fd3169 2024-03-19 stsp
736 05fd3169 2024-03-19 stsp return err;
737 05fd3169 2024-03-19 stsp }
738 05fd3169 2024-03-19 stsp
739 05fd3169 2024-03-19 stsp static const struct got_error *
740 05fd3169 2024-03-19 stsp open_blob(struct got_blob_object **blob, struct got_repository *repo,
741 05fd3169 2024-03-19 stsp struct got_object_id *id, size_t blocksize, int outfd)
742 05fd3169 2024-03-19 stsp {
743 05fd3169 2024-03-19 stsp const struct got_error *err = NULL;
744 05fd3169 2024-03-19 stsp struct got_packidx *packidx = NULL;
745 05fd3169 2024-03-19 stsp int idx, dfd = -1;
746 05fd3169 2024-03-19 stsp char *path_packfile = NULL;
747 05fd3169 2024-03-19 stsp uint8_t *outbuf;
748 05fd3169 2024-03-19 stsp size_t size, hdrlen;
749 05fd3169 2024-03-19 stsp struct stat sb;
750 05fd3169 2024-03-19 stsp
751 05fd3169 2024-03-19 stsp *blob = calloc(1, sizeof(**blob));
752 05fd3169 2024-03-19 stsp if (*blob == NULL)
753 05fd3169 2024-03-19 stsp return got_error_from_errno("calloc");
754 05fd3169 2024-03-19 stsp
755 05fd3169 2024-03-19 stsp (*blob)->read_buf = malloc(blocksize);
756 05fd3169 2024-03-19 stsp if ((*blob)->read_buf == NULL) {
757 05fd3169 2024-03-19 stsp err = got_error_from_errno("malloc");
758 05fd3169 2024-03-19 stsp goto done;
759 05fd3169 2024-03-19 stsp }
760 05fd3169 2024-03-19 stsp
761 05fd3169 2024-03-19 stsp if (ftruncate(outfd, 0L) == -1) {
762 05fd3169 2024-03-19 stsp err = got_error_from_errno("ftruncate");
763 05fd3169 2024-03-19 stsp goto done;
764 05fd3169 2024-03-19 stsp }
765 05fd3169 2024-03-19 stsp if (lseek(outfd, SEEK_SET, 0) == -1) {
766 05fd3169 2024-03-19 stsp err = got_error_from_errno("lseek");
767 05fd3169 2024-03-19 stsp goto done;
768 05fd3169 2024-03-19 stsp }
769 05fd3169 2024-03-19 stsp
770 05fd3169 2024-03-19 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
771 05fd3169 2024-03-19 stsp if (err == NULL) {
772 05fd3169 2024-03-19 stsp struct got_pack *pack = NULL;
773 05fd3169 2024-03-19 stsp
774 05fd3169 2024-03-19 stsp err = got_packidx_get_packfile_path(&path_packfile,
775 05fd3169 2024-03-19 stsp packidx->path_packidx);
776 05fd3169 2024-03-19 stsp if (err)
777 05fd3169 2024-03-19 stsp goto done;
778 05fd3169 2024-03-19 stsp
779 05fd3169 2024-03-19 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
780 05fd3169 2024-03-19 stsp if (pack == NULL) {
781 05fd3169 2024-03-19 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
782 05fd3169 2024-03-19 stsp packidx);
783 05fd3169 2024-03-19 stsp if (err)
784 05fd3169 2024-03-19 stsp goto done;
785 05fd3169 2024-03-19 stsp }
786 05fd3169 2024-03-19 stsp err = read_packed_blob(&outbuf, &size, &hdrlen, outfd,
787 05fd3169 2024-03-19 stsp pack, packidx, idx, id, repo);
788 05fd3169 2024-03-19 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
789 05fd3169 2024-03-19 stsp int infd;
790 05fd3169 2024-03-19 stsp
791 05fd3169 2024-03-19 stsp err = got_object_open_loose_fd(&infd, id, repo);
792 05fd3169 2024-03-19 stsp if (err)
793 05fd3169 2024-03-19 stsp goto done;
794 05fd3169 2024-03-19 stsp err = read_blob(&outbuf, &size, &hdrlen, outfd, infd,
795 05fd3169 2024-03-19 stsp id, repo);
796 05fd3169 2024-03-19 stsp }
797 05fd3169 2024-03-19 stsp if (err)
798 05fd3169 2024-03-19 stsp goto done;
799 05fd3169 2024-03-19 stsp
800 05fd3169 2024-03-19 stsp if (hdrlen > size) {
801 05fd3169 2024-03-19 stsp err = got_error(GOT_ERR_BAD_OBJ_HDR);
802 05fd3169 2024-03-19 stsp goto done;
803 05fd3169 2024-03-19 stsp }
804 05fd3169 2024-03-19 stsp
805 05fd3169 2024-03-19 stsp if (outbuf) {
806 05fd3169 2024-03-19 stsp (*blob)->f = fmemopen(outbuf, size, "rb");
807 05fd3169 2024-03-19 stsp if ((*blob)->f == NULL) {
808 05fd3169 2024-03-19 stsp err = got_error_from_errno("fmemopen");
809 05fd3169 2024-03-19 stsp free(outbuf);
810 05fd3169 2024-03-19 stsp goto done;
811 05fd3169 2024-03-19 stsp }
812 05fd3169 2024-03-19 stsp (*blob)->data = outbuf;
813 05fd3169 2024-03-19 stsp } else {
814 05fd3169 2024-03-19 stsp if (fstat(outfd, &sb) == -1) {
815 05fd3169 2024-03-19 stsp err = got_error_from_errno("fstat");
816 05fd3169 2024-03-19 stsp goto done;
817 05fd3169 2024-03-19 stsp }
818 05fd3169 2024-03-19 stsp
819 05fd3169 2024-03-19 stsp if (sb.st_size != size) {
820 05fd3169 2024-03-19 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
821 05fd3169 2024-03-19 stsp goto done;
822 05fd3169 2024-03-19 stsp }
823 05fd3169 2024-03-19 stsp
824 05fd3169 2024-03-19 stsp dfd = dup(outfd);
825 05fd3169 2024-03-19 stsp if (dfd == -1) {
826 05fd3169 2024-03-19 stsp err = got_error_from_errno("dup");
827 05fd3169 2024-03-19 stsp goto done;
828 05fd3169 2024-03-19 stsp }
829 05fd3169 2024-03-19 stsp
830 05fd3169 2024-03-19 stsp (*blob)->f = fdopen(dfd, "rb");
831 05fd3169 2024-03-19 stsp if ((*blob)->f == NULL) {
832 05fd3169 2024-03-19 stsp err = got_error_from_errno("fdopen");
833 05fd3169 2024-03-19 stsp close(dfd);
834 05fd3169 2024-03-19 stsp dfd = -1;
835 05fd3169 2024-03-19 stsp goto done;
836 05fd3169 2024-03-19 stsp }
837 05fd3169 2024-03-19 stsp }
838 05fd3169 2024-03-19 stsp
839 05fd3169 2024-03-19 stsp (*blob)->hdrlen = hdrlen;
840 05fd3169 2024-03-19 stsp (*blob)->blocksize = blocksize;
841 05fd3169 2024-03-19 stsp memcpy(&(*blob)->id, id, sizeof(*id));
842 05fd3169 2024-03-19 stsp
843 05fd3169 2024-03-19 stsp done:
844 05fd3169 2024-03-19 stsp free(path_packfile);
845 05fd3169 2024-03-19 stsp if (err) {
846 05fd3169 2024-03-19 stsp if (*blob) {
847 05fd3169 2024-03-19 stsp got_object_blob_close(*blob);
848 05fd3169 2024-03-19 stsp *blob = NULL;
849 05fd3169 2024-03-19 stsp }
850 05fd3169 2024-03-19 stsp }
851 05fd3169 2024-03-19 stsp return err;
852 05fd3169 2024-03-19 stsp }
853 05fd3169 2024-03-19 stsp
854 13b2bc37 2022-10-23 stsp const struct got_error *
855 13b2bc37 2022-10-23 stsp got_object_open_as_blob(struct got_blob_object **blob,
856 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id, size_t blocksize,
857 13b2bc37 2022-10-23 stsp int outfd)
858 13b2bc37 2022-10-23 stsp {
859 05fd3169 2024-03-19 stsp return open_blob(blob, repo, id, blocksize, outfd);
860 13b2bc37 2022-10-23 stsp }
861 13b2bc37 2022-10-23 stsp
862 13b2bc37 2022-10-23 stsp const struct got_error *
863 13b2bc37 2022-10-23 stsp got_object_blob_open(struct got_blob_object **blob,
864 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj, size_t blocksize,
865 13b2bc37 2022-10-23 stsp int outfd)
866 13b2bc37 2022-10-23 stsp {
867 05fd3169 2024-03-19 stsp return open_blob(blob, repo, got_object_get_id(obj), blocksize, outfd);
868 13b2bc37 2022-10-23 stsp }
869 13b2bc37 2022-10-23 stsp
870 13b2bc37 2022-10-23 stsp static const struct got_error *
871 13b2bc37 2022-10-23 stsp open_tag(struct got_tag_object **tag, struct got_repository *repo,
872 13b2bc37 2022-10-23 stsp struct got_object_id *id, int check_cache)
873 13b2bc37 2022-10-23 stsp {
874 13b2bc37 2022-10-23 stsp const struct got_error *err = NULL;
875 13b2bc37 2022-10-23 stsp struct got_packidx *packidx = NULL;
876 13b2bc37 2022-10-23 stsp int idx;
877 13b2bc37 2022-10-23 stsp char *path_packfile = NULL;
878 13b2bc37 2022-10-23 stsp struct got_object *obj = NULL;
879 13b2bc37 2022-10-23 stsp int obj_type = GOT_OBJ_TYPE_ANY;
880 13b2bc37 2022-10-23 stsp
881 13b2bc37 2022-10-23 stsp if (check_cache) {
882 13b2bc37 2022-10-23 stsp *tag = got_repo_get_cached_tag(repo, id);
883 13b2bc37 2022-10-23 stsp if (*tag != NULL) {
884 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
885 13b2bc37 2022-10-23 stsp return NULL;
886 13b2bc37 2022-10-23 stsp }
887 13b2bc37 2022-10-23 stsp } else
888 13b2bc37 2022-10-23 stsp *tag = NULL;
889 13b2bc37 2022-10-23 stsp
890 13b2bc37 2022-10-23 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
891 13b2bc37 2022-10-23 stsp if (err == NULL) {
892 13b2bc37 2022-10-23 stsp struct got_pack *pack = NULL;
893 13b2bc37 2022-10-23 stsp uint8_t *buf = NULL;
894 13b2bc37 2022-10-23 stsp size_t len;
895 13b2bc37 2022-10-23 stsp
896 13b2bc37 2022-10-23 stsp err = got_packidx_get_packfile_path(&path_packfile,
897 13b2bc37 2022-10-23 stsp packidx->path_packidx);
898 13b2bc37 2022-10-23 stsp if (err)
899 13b2bc37 2022-10-23 stsp return err;
900 13b2bc37 2022-10-23 stsp
901 13b2bc37 2022-10-23 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
902 13b2bc37 2022-10-23 stsp if (pack == NULL) {
903 13b2bc37 2022-10-23 stsp err = got_repo_cache_pack(&pack, repo, path_packfile,
904 13b2bc37 2022-10-23 stsp packidx);
905 13b2bc37 2022-10-23 stsp if (err)
906 13b2bc37 2022-10-23 stsp goto done;
907 13b2bc37 2022-10-23 stsp }
908 13b2bc37 2022-10-23 stsp
909 13b2bc37 2022-10-23 stsp /* Beware of "lightweight" tags: Check object type first. */
910 13b2bc37 2022-10-23 stsp err = got_packfile_open_object(&obj, pack, packidx, idx, id);
911 13b2bc37 2022-10-23 stsp if (err)
912 13b2bc37 2022-10-23 stsp goto done;
913 13b2bc37 2022-10-23 stsp obj_type = obj->type;
914 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_TAG) {
915 13b2bc37 2022-10-23 stsp err = got_error(GOT_ERR_OBJ_TYPE);
916 13b2bc37 2022-10-23 stsp got_object_close(obj);
917 13b2bc37 2022-10-23 stsp goto done;
918 13b2bc37 2022-10-23 stsp }
919 13b2bc37 2022-10-23 stsp err = got_packfile_extract_object_to_mem(&buf, &len,
920 13b2bc37 2022-10-23 stsp obj, pack);
921 13b2bc37 2022-10-23 stsp got_object_close(obj);
922 13b2bc37 2022-10-23 stsp if (err)
923 13b2bc37 2022-10-23 stsp goto done;
924 13b2bc37 2022-10-23 stsp err = got_object_parse_tag(tag, buf, len);
925 13b2bc37 2022-10-23 stsp free(buf);
926 13b2bc37 2022-10-23 stsp } else if (err->code == GOT_ERR_NO_OBJ) {
927 13b2bc37 2022-10-23 stsp int fd;
928 13b2bc37 2022-10-23 stsp
929 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
930 13b2bc37 2022-10-23 stsp if (err)
931 13b2bc37 2022-10-23 stsp return err;
932 13b2bc37 2022-10-23 stsp err = got_object_read_header(&obj, fd);
933 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
934 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
935 13b2bc37 2022-10-23 stsp if (err)
936 13b2bc37 2022-10-23 stsp return err;
937 13b2bc37 2022-10-23 stsp obj_type = obj->type;
938 13b2bc37 2022-10-23 stsp got_object_close(obj);
939 13b2bc37 2022-10-23 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
940 13b2bc37 2022-10-23 stsp return got_error(GOT_ERR_OBJ_TYPE);
941 13b2bc37 2022-10-23 stsp
942 13b2bc37 2022-10-23 stsp err = got_object_open_loose_fd(&fd, id, repo);
943 13b2bc37 2022-10-23 stsp if (err)
944 13b2bc37 2022-10-23 stsp return err;
945 13b2bc37 2022-10-23 stsp err = got_object_read_tag(tag, fd, id, 0);
946 13b2bc37 2022-10-23 stsp if (close(fd) == -1 && err == NULL)
947 13b2bc37 2022-10-23 stsp err = got_error_from_errno("close");
948 13b2bc37 2022-10-23 stsp if (err)
949 13b2bc37 2022-10-23 stsp return err;
950 13b2bc37 2022-10-23 stsp }
951 13b2bc37 2022-10-23 stsp
952 13b2bc37 2022-10-23 stsp if (err == NULL) {
953 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
954 13b2bc37 2022-10-23 stsp err = got_repo_cache_tag(repo, id, *tag);
955 13b2bc37 2022-10-23 stsp if (err) {
956 13b2bc37 2022-10-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
957 13b2bc37 2022-10-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
958 13b2bc37 2022-10-23 stsp err = NULL;
959 13b2bc37 2022-10-23 stsp }
960 13b2bc37 2022-10-23 stsp }
961 13b2bc37 2022-10-23 stsp done:
962 13b2bc37 2022-10-23 stsp free(path_packfile);
963 13b2bc37 2022-10-23 stsp return err;
964 13b2bc37 2022-10-23 stsp }
965 13b2bc37 2022-10-23 stsp
966 13b2bc37 2022-10-23 stsp const struct got_error *
967 13b2bc37 2022-10-23 stsp got_object_open_as_tag(struct got_tag_object **tag,
968 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object_id *id)
969 13b2bc37 2022-10-23 stsp {
970 13b2bc37 2022-10-23 stsp *tag = got_repo_get_cached_tag(repo, id);
971 13b2bc37 2022-10-23 stsp if (*tag != NULL) {
972 13b2bc37 2022-10-23 stsp (*tag)->refcnt++;
973 13b2bc37 2022-10-23 stsp return NULL;
974 13b2bc37 2022-10-23 stsp }
975 13b2bc37 2022-10-23 stsp
976 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, id, 0);
977 13b2bc37 2022-10-23 stsp }
978 13b2bc37 2022-10-23 stsp
979 13b2bc37 2022-10-23 stsp const struct got_error *
980 13b2bc37 2022-10-23 stsp got_object_tag_open(struct got_tag_object **tag,
981 13b2bc37 2022-10-23 stsp struct got_repository *repo, struct got_object *obj)
982 13b2bc37 2022-10-23 stsp {
983 13b2bc37 2022-10-23 stsp return open_tag(tag, repo, got_object_get_id(obj), 1);
984 13b2bc37 2022-10-23 stsp }