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