Blame


1 0a0a3048 2018-01-10 stsp /*
2 0a0a3048 2018-01-10 stsp * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
3 0a0a3048 2018-01-10 stsp *
4 0a0a3048 2018-01-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 0a0a3048 2018-01-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 0a0a3048 2018-01-10 stsp * copyright notice and this permission notice appear in all copies.
7 0a0a3048 2018-01-10 stsp *
8 0a0a3048 2018-01-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 0a0a3048 2018-01-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 0a0a3048 2018-01-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 0a0a3048 2018-01-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 0a0a3048 2018-01-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 0a0a3048 2018-01-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 0a0a3048 2018-01-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 0a0a3048 2018-01-10 stsp */
16 0a0a3048 2018-01-10 stsp
17 a1fd68d8 2018-01-12 stsp #include <sys/types.h>
18 0a0a3048 2018-01-10 stsp #include <sys/stat.h>
19 a1fd68d8 2018-01-12 stsp #include <sys/queue.h>
20 0a0a3048 2018-01-10 stsp
21 a1fd68d8 2018-01-12 stsp #include <dirent.h>
22 7e656b93 2018-03-17 stsp #include <fcntl.h>
23 a1fd68d8 2018-01-12 stsp #include <errno.h>
24 0a0a3048 2018-01-10 stsp #include <stdio.h>
25 a1fd68d8 2018-01-12 stsp #include <stdint.h>
26 0a0a3048 2018-01-10 stsp #include <stdlib.h>
27 0a0a3048 2018-01-10 stsp #include <string.h>
28 0a0a3048 2018-01-10 stsp #include <limits.h>
29 0a0a3048 2018-01-10 stsp #include <sha1.h>
30 0a0a3048 2018-01-10 stsp #include <endian.h>
31 a1fd68d8 2018-01-12 stsp #include <zlib.h>
32 0a0a3048 2018-01-10 stsp
33 0a0a3048 2018-01-10 stsp #include "got_error.h"
34 a1fd68d8 2018-01-12 stsp #include "got_object.h"
35 a1fd68d8 2018-01-12 stsp #include "got_repository.h"
36 0a0a3048 2018-01-10 stsp
37 718b3ab0 2018-03-17 stsp #include "got_lib_sha1.h"
38 718b3ab0 2018-03-17 stsp #include "got_lib_pack.h"
39 718b3ab0 2018-03-17 stsp #include "got_lib_path.h"
40 718b3ab0 2018-03-17 stsp #include "got_lib_delta.h"
41 718b3ab0 2018-03-17 stsp #include "got_lib_zbuf.h"
42 718b3ab0 2018-03-17 stsp #include "got_lib_object.h"
43 718b3ab0 2018-03-17 stsp #include "got_lib_repository.h"
44 79b11c62 2018-03-09 stsp
45 79b11c62 2018-03-09 stsp #ifndef nitems
46 79b11c62 2018-03-09 stsp #define nitems(_a) (sizeof(_a) / sizeof((_a)[0]))
47 79b11c62 2018-03-09 stsp #endif
48 1411938b 2018-02-12 stsp
49 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
50 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
51 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
52 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
53 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
54 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
55 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
56 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
57 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
58 a1fd68d8 2018-01-12 stsp
59 a1fd68d8 2018-01-12 stsp #ifndef MIN
60 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 a1fd68d8 2018-01-12 stsp #endif
62 a1fd68d8 2018-01-12 stsp
63 0a0a3048 2018-01-10 stsp static const struct got_error *
64 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
65 0a0a3048 2018-01-10 stsp {
66 0a0a3048 2018-01-10 stsp int i;
67 0a0a3048 2018-01-10 stsp
68 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
69 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
70 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
71 0a0a3048 2018-01-10 stsp }
72 0a0a3048 2018-01-10 stsp
73 0a0a3048 2018-01-10 stsp return NULL;
74 0a0a3048 2018-01-10 stsp }
75 0a0a3048 2018-01-10 stsp
76 24541888 2018-01-10 stsp static const struct got_error *
77 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
78 0a0a3048 2018-01-10 stsp {
79 0a0a3048 2018-01-10 stsp struct stat sb;
80 0a0a3048 2018-01-10 stsp char *path_pack;
81 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
82 0a0a3048 2018-01-10 stsp char *dot;
83 0a0a3048 2018-01-10 stsp
84 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
85 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
86 0a0a3048 2018-01-10 stsp
87 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
88 0a0a3048 2018-01-10 stsp if (dot == NULL)
89 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
90 0a0a3048 2018-01-10 stsp *dot = '\0';
91 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
92 0a585a0d 2018-03-17 stsp return got_error_from_errno();
93 0a0a3048 2018-01-10 stsp
94 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
95 0a0a3048 2018-01-10 stsp free(path_pack);
96 8251fdbc 2018-01-12 stsp return got_error_from_errno();
97 0a0a3048 2018-01-10 stsp }
98 0a0a3048 2018-01-10 stsp
99 0a0a3048 2018-01-10 stsp free(path_pack);
100 0a0a3048 2018-01-10 stsp *size = sb.st_size;
101 0a0a3048 2018-01-10 stsp return 0;
102 0a0a3048 2018-01-10 stsp }
103 0a0a3048 2018-01-10 stsp
104 0a0a3048 2018-01-10 stsp const struct got_error *
105 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
106 0a0a3048 2018-01-10 stsp {
107 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
108 0a0a3048 2018-01-10 stsp FILE *f;
109 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
110 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
111 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
112 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
113 0a0a3048 2018-01-10 stsp
114 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
115 0ebaf008 2018-01-10 stsp
116 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
117 0a0a3048 2018-01-10 stsp if (f == NULL)
118 9feb4ff2 2018-03-14 stsp return got_error_from_errno();
119 0a0a3048 2018-01-10 stsp
120 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
121 0a0a3048 2018-01-10 stsp if (err)
122 0a0a3048 2018-01-10 stsp return err;
123 0a0a3048 2018-01-10 stsp
124 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
125 0a0a3048 2018-01-10 stsp if (p == NULL) {
126 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
127 0a0a3048 2018-01-10 stsp goto done;
128 0a0a3048 2018-01-10 stsp }
129 0a0a3048 2018-01-10 stsp
130 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
131 0a0a3048 2018-01-10 stsp if (n != 1) {
132 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
133 0a0a3048 2018-01-10 stsp goto done;
134 0a0a3048 2018-01-10 stsp }
135 0a0a3048 2018-01-10 stsp
136 0a0a3048 2018-01-10 stsp if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
137 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
138 0a0a3048 2018-01-10 stsp goto done;
139 0a0a3048 2018-01-10 stsp }
140 0a0a3048 2018-01-10 stsp
141 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
142 0ebaf008 2018-01-10 stsp
143 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
144 0a0a3048 2018-01-10 stsp if (n != 1) {
145 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
146 0a0a3048 2018-01-10 stsp goto done;
147 0a0a3048 2018-01-10 stsp }
148 0a0a3048 2018-01-10 stsp
149 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
150 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
151 0a0a3048 2018-01-10 stsp goto done;
152 0a0a3048 2018-01-10 stsp }
153 0a0a3048 2018-01-10 stsp
154 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
155 0ebaf008 2018-01-10 stsp
156 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
157 0a0a3048 2018-01-10 stsp if (n != 1) {
158 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
159 0a0a3048 2018-01-10 stsp goto done;
160 0a0a3048 2018-01-10 stsp }
161 0a0a3048 2018-01-10 stsp
162 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
163 0a0a3048 2018-01-10 stsp if (err)
164 0a0a3048 2018-01-10 stsp goto done;
165 0a0a3048 2018-01-10 stsp
166 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
167 0ebaf008 2018-01-10 stsp
168 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
169 0a0a3048 2018-01-10 stsp
170 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
171 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
172 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
173 0a0a3048 2018-01-10 stsp goto done;
174 0a0a3048 2018-01-10 stsp }
175 0a0a3048 2018-01-10 stsp
176 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
177 0a0a3048 2018-01-10 stsp if (n != nobj) {
178 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
179 0a0a3048 2018-01-10 stsp goto done;
180 0a0a3048 2018-01-10 stsp }
181 0a0a3048 2018-01-10 stsp
182 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
183 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
184 0ebaf008 2018-01-10 stsp
185 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
186 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
187 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
188 0a0a3048 2018-01-10 stsp goto done;
189 0a0a3048 2018-01-10 stsp }
190 0a0a3048 2018-01-10 stsp
191 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
192 0a0a3048 2018-01-10 stsp if (n != nobj) {
193 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
194 0a0a3048 2018-01-10 stsp goto done;
195 0a0a3048 2018-01-10 stsp }
196 0a0a3048 2018-01-10 stsp
197 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
198 0ebaf008 2018-01-10 stsp
199 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
200 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
201 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
202 0a0a3048 2018-01-10 stsp goto done;
203 0a0a3048 2018-01-10 stsp }
204 0a0a3048 2018-01-10 stsp
205 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
206 0a0a3048 2018-01-10 stsp if (n != nobj) {
207 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
208 0a0a3048 2018-01-10 stsp goto done;
209 0a0a3048 2018-01-10 stsp }
210 0a0a3048 2018-01-10 stsp
211 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
212 0ebaf008 2018-01-10 stsp
213 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
214 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
215 0a0a3048 2018-01-10 stsp goto checksum;
216 0a0a3048 2018-01-10 stsp
217 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
218 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
219 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
220 0a0a3048 2018-01-10 stsp goto done;
221 0a0a3048 2018-01-10 stsp }
222 0a0a3048 2018-01-10 stsp
223 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
224 0a0a3048 2018-01-10 stsp if (n != nobj) {
225 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
226 0a0a3048 2018-01-10 stsp goto done;
227 0a0a3048 2018-01-10 stsp }
228 0a0a3048 2018-01-10 stsp
229 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
230 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
231 0ebaf008 2018-01-10 stsp
232 0a0a3048 2018-01-10 stsp checksum:
233 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
234 0a0a3048 2018-01-10 stsp if (n != 1) {
235 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
236 0a0a3048 2018-01-10 stsp goto done;
237 0a0a3048 2018-01-10 stsp }
238 0a0a3048 2018-01-10 stsp
239 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
240 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
241 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
242 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
243 0a0a3048 2018-01-10 stsp done:
244 0a0a3048 2018-01-10 stsp fclose(f);
245 0a0a3048 2018-01-10 stsp if (err)
246 0a0a3048 2018-01-10 stsp got_packidx_close(p);
247 0a0a3048 2018-01-10 stsp else
248 0a0a3048 2018-01-10 stsp *packidx = p;
249 0a0a3048 2018-01-10 stsp return err;
250 0a0a3048 2018-01-10 stsp }
251 0a0a3048 2018-01-10 stsp
252 0a0a3048 2018-01-10 stsp void
253 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
254 0a0a3048 2018-01-10 stsp {
255 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
256 0a0a3048 2018-01-10 stsp free(packidx->offsets);
257 0a0a3048 2018-01-10 stsp free(packidx->crc32);
258 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
259 0a0a3048 2018-01-10 stsp free(packidx);
260 a1fd68d8 2018-01-12 stsp }
261 a1fd68d8 2018-01-12 stsp
262 a1fd68d8 2018-01-12 stsp static int
263 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
264 a1fd68d8 2018-01-12 stsp {
265 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
266 a1fd68d8 2018-01-12 stsp return 0;
267 a1fd68d8 2018-01-12 stsp
268 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
269 a1fd68d8 2018-01-12 stsp return 0;
270 a1fd68d8 2018-01-12 stsp
271 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
272 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
273 a1fd68d8 2018-01-12 stsp return 0;
274 a1fd68d8 2018-01-12 stsp
275 a1fd68d8 2018-01-12 stsp return 1;
276 a1fd68d8 2018-01-12 stsp }
277 a1fd68d8 2018-01-12 stsp
278 a1fd68d8 2018-01-12 stsp static off_t
279 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
280 a1fd68d8 2018-01-12 stsp {
281 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
282 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
283 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
284 a1fd68d8 2018-01-12 stsp uint64_t loffset;
285 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
286 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
287 a1fd68d8 2018-01-12 stsp return -1;
288 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
289 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
290 a1fd68d8 2018-01-12 stsp }
291 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
292 a1fd68d8 2018-01-12 stsp }
293 72eb3431 2018-04-01 stsp
294 72eb3431 2018-04-01 stsp static const struct got_error *
295 72eb3431 2018-04-01 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
296 72eb3431 2018-04-01 stsp struct got_packidx_v2_hdr *packidx);
297 a1fd68d8 2018-01-12 stsp
298 a1fd68d8 2018-01-12 stsp static int
299 72eb3431 2018-04-01 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id, struct got_repository *repo)
300 a1fd68d8 2018-01-12 stsp {
301 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
302 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
303 a1fd68d8 2018-01-12 stsp int i = 0;
304 72eb3431 2018-04-01 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
305 72eb3431 2018-04-01 stsp char *sha1str = got_sha1_digest_to_str(id->sha1, hex, sizeof(hex));
306 a1fd68d8 2018-01-12 stsp
307 a1fd68d8 2018-01-12 stsp if (id0 > 0)
308 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
309 a1fd68d8 2018-01-12 stsp
310 a1fd68d8 2018-01-12 stsp while (i < totobj) {
311 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
312 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
313 a1fd68d8 2018-01-12 stsp
314 72eb3431 2018-04-01 stsp if (cmp == 0) {
315 72eb3431 2018-04-01 stsp char *path_packfile = NULL;
316 72eb3431 2018-04-01 stsp const struct got_error *err;
317 72eb3431 2018-04-01 stsp err = get_packfile_path(&path_packfile, repo, packidx);
318 72eb3431 2018-04-01 stsp if (err) {
319 72eb3431 2018-04-01 stsp printf("get_packfile_path: %s\n", err->msg);
320 72eb3431 2018-04-01 stsp } else {
321 72eb3431 2018-04-01 stsp if (strcmp(sha1str, "b3197d9ea53a42fc1632369008b8f3a085dcd205") == 0) {
322 72eb3431 2018-04-01 stsp printf("Found %s in %s\n", sha1str, path_packfile);
323 72eb3431 2018-04-01 stsp }
324 72eb3431 2018-04-01 stsp }
325 72eb3431 2018-04-01 stsp free(path_packfile);
326 6c00b545 2018-01-17 stsp return i;
327 72eb3431 2018-04-01 stsp }
328 6c00b545 2018-01-17 stsp i++;
329 a1fd68d8 2018-01-12 stsp }
330 a1fd68d8 2018-01-12 stsp
331 a1fd68d8 2018-01-12 stsp return -1;
332 79b11c62 2018-03-09 stsp }
333 79b11c62 2018-03-09 stsp
334 79b11c62 2018-03-09 stsp static struct got_packidx_v2_hdr *
335 79b11c62 2018-03-09 stsp dup_packidx(struct got_packidx_v2_hdr *packidx)
336 79b11c62 2018-03-09 stsp {
337 79b11c62 2018-03-09 stsp struct got_packidx_v2_hdr *p;
338 79b11c62 2018-03-09 stsp size_t nobj;
339 79b11c62 2018-03-09 stsp
340 79b11c62 2018-03-09 stsp p = calloc(1, sizeof(*p));
341 79b11c62 2018-03-09 stsp if (p == NULL)
342 79b11c62 2018-03-09 stsp return NULL;
343 79b11c62 2018-03-09 stsp
344 79b11c62 2018-03-09 stsp memcpy(p, packidx, sizeof(*p));
345 79b11c62 2018-03-09 stsp p->sorted_ids = NULL;
346 79b11c62 2018-03-09 stsp p->crc32 = NULL;
347 79b11c62 2018-03-09 stsp p->offsets = NULL;
348 79b11c62 2018-03-09 stsp p->large_offsets = NULL;
349 79b11c62 2018-03-09 stsp
350 79b11c62 2018-03-09 stsp nobj = betoh32(p->fanout_table[0xff]);
351 79b11c62 2018-03-09 stsp
352 79b11c62 2018-03-09 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
353 79b11c62 2018-03-09 stsp if (p->sorted_ids == NULL)
354 79b11c62 2018-03-09 stsp goto err;
355 79b11c62 2018-03-09 stsp memcpy(p->sorted_ids, packidx->sorted_ids, nobj * sizeof(*p->sorted_ids));
356 79b11c62 2018-03-09 stsp
357 79b11c62 2018-03-09 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
358 79b11c62 2018-03-09 stsp if (p->crc32 == NULL)
359 79b11c62 2018-03-09 stsp goto err;
360 79b11c62 2018-03-09 stsp memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
361 79b11c62 2018-03-09 stsp
362 79b11c62 2018-03-09 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
363 79b11c62 2018-03-09 stsp if (p->offsets == NULL)
364 79b11c62 2018-03-09 stsp goto err;
365 79b11c62 2018-03-09 stsp memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
366 79b11c62 2018-03-09 stsp
367 79b11c62 2018-03-09 stsp if (p->large_offsets) {
368 79b11c62 2018-03-09 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
369 79b11c62 2018-03-09 stsp if (p->large_offsets == NULL)
370 79b11c62 2018-03-09 stsp goto err;
371 79b11c62 2018-03-09 stsp memcpy(p->large_offsets, packidx->large_offsets,
372 79b11c62 2018-03-09 stsp nobj * sizeof(*p->large_offsets));
373 79b11c62 2018-03-09 stsp }
374 79b11c62 2018-03-09 stsp
375 79b11c62 2018-03-09 stsp return p;
376 79b11c62 2018-03-09 stsp
377 79b11c62 2018-03-09 stsp err:
378 79b11c62 2018-03-09 stsp free(p->large_offsets);
379 79b11c62 2018-03-09 stsp free(p->offsets);
380 79b11c62 2018-03-09 stsp free(p->crc32);
381 79b11c62 2018-03-09 stsp free(p->sorted_ids);
382 79b11c62 2018-03-09 stsp free(p);
383 79b11c62 2018-03-09 stsp return NULL;
384 87c99799 2018-03-16 stsp }
385 87c99799 2018-03-16 stsp
386 65cf1e80 2018-03-16 stsp static void
387 65cf1e80 2018-03-16 stsp cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
388 87c99799 2018-03-16 stsp {
389 79b11c62 2018-03-09 stsp int i;
390 79b11c62 2018-03-09 stsp
391 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
392 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
393 79b11c62 2018-03-09 stsp break;
394 79b11c62 2018-03-09 stsp }
395 79b11c62 2018-03-09 stsp
396 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
397 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i - 1]);
398 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
399 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
400 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
401 79b11c62 2018-03-09 stsp i = 0;
402 79b11c62 2018-03-09 stsp }
403 79b11c62 2018-03-09 stsp
404 65cf1e80 2018-03-16 stsp repo->packidx_cache[i] = dup_packidx(packidx);
405 79b11c62 2018-03-09 stsp }
406 79b11c62 2018-03-09 stsp
407 6b9c9673 2018-01-23 stsp static const struct got_error *
408 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
409 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
410 6b9c9673 2018-01-23 stsp {
411 6b9c9673 2018-01-23 stsp const struct got_error *err;
412 6b9c9673 2018-01-23 stsp char *path_packdir;
413 6b9c9673 2018-01-23 stsp DIR *packdir;
414 6b9c9673 2018-01-23 stsp struct dirent *dent;
415 6b9c9673 2018-01-23 stsp char *path_packidx;
416 79b11c62 2018-03-09 stsp int i;
417 6b9c9673 2018-01-23 stsp
418 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
419 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
420 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
421 79b11c62 2018-03-09 stsp break;
422 72eb3431 2018-04-01 stsp *idx = get_object_idx(repo->packidx_cache[i], id, repo);
423 79b11c62 2018-03-09 stsp if (*idx != -1) {
424 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
425 79b11c62 2018-03-09 stsp return NULL;
426 79b11c62 2018-03-09 stsp }
427 79b11c62 2018-03-09 stsp }
428 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
429 79b11c62 2018-03-09 stsp
430 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
431 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
432 0a585a0d 2018-03-17 stsp return got_error_from_errno();
433 6b9c9673 2018-01-23 stsp
434 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
435 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
436 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
437 6b9c9673 2018-01-23 stsp goto done;
438 6b9c9673 2018-01-23 stsp }
439 6b9c9673 2018-01-23 stsp
440 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
441 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
442 6b9c9673 2018-01-23 stsp continue;
443 6b9c9673 2018-01-23 stsp
444 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
445 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
446 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
447 6b9c9673 2018-01-23 stsp goto done;
448 6b9c9673 2018-01-23 stsp }
449 6b9c9673 2018-01-23 stsp
450 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
451 6b9c9673 2018-01-23 stsp free(path_packidx);
452 6b9c9673 2018-01-23 stsp if (err)
453 6b9c9673 2018-01-23 stsp goto done;
454 6b9c9673 2018-01-23 stsp
455 72eb3431 2018-04-01 stsp *idx = get_object_idx(*packidx, id, repo);
456 6b9c9673 2018-01-23 stsp if (*idx != -1) {
457 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
458 65cf1e80 2018-03-16 stsp cache_packidx(*packidx, repo);
459 6b9c9673 2018-01-23 stsp goto done;
460 6b9c9673 2018-01-23 stsp }
461 6b9c9673 2018-01-23 stsp
462 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
463 6b9c9673 2018-01-23 stsp *packidx = NULL;
464 6b9c9673 2018-01-23 stsp }
465 6b9c9673 2018-01-23 stsp
466 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
467 6b9c9673 2018-01-23 stsp done:
468 6b9c9673 2018-01-23 stsp free(path_packdir);
469 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
470 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
471 6b9c9673 2018-01-23 stsp return err;
472 6b9c9673 2018-01-23 stsp }
473 6b9c9673 2018-01-23 stsp
474 c7fe698a 2018-01-23 stsp static const struct got_error *
475 65cf1e80 2018-03-16 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
476 65cf1e80 2018-03-16 stsp struct got_packidx_v2_hdr *packidx)
477 c7fe698a 2018-01-23 stsp {
478 65cf1e80 2018-03-16 stsp char *path_packdir;
479 65cf1e80 2018-03-16 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
480 65cf1e80 2018-03-16 stsp char *sha1str;
481 c7fe698a 2018-01-23 stsp
482 65cf1e80 2018-03-16 stsp path_packdir = got_repo_get_path_objects_pack(repo);
483 65cf1e80 2018-03-16 stsp if (path_packdir == NULL)
484 0a585a0d 2018-03-17 stsp return got_error_from_errno();
485 c7fe698a 2018-01-23 stsp
486 65cf1e80 2018-03-16 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
487 65cf1e80 2018-03-16 stsp hex, sizeof(hex));
488 65cf1e80 2018-03-16 stsp if (sha1str == NULL)
489 65cf1e80 2018-03-16 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
490 87c99799 2018-03-16 stsp
491 65cf1e80 2018-03-16 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
492 65cf1e80 2018-03-16 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
493 65cf1e80 2018-03-16 stsp *path_packfile = NULL;
494 0a585a0d 2018-03-17 stsp return got_error_from_errno();
495 87c99799 2018-03-16 stsp }
496 87c99799 2018-03-16 stsp
497 65cf1e80 2018-03-16 stsp return NULL;
498 65cf1e80 2018-03-16 stsp }
499 65cf1e80 2018-03-16 stsp
500 65cf1e80 2018-03-16 stsp const struct got_error *
501 65cf1e80 2018-03-16 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
502 65cf1e80 2018-03-16 stsp {
503 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
504 65cf1e80 2018-03-16 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
505 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
506 65cf1e80 2018-03-16 stsp size_t n;
507 65cf1e80 2018-03-16 stsp
508 65cf1e80 2018-03-16 stsp n = fread(&hdr, sizeof(hdr), 1, f);
509 65cf1e80 2018-03-16 stsp if (n != 1)
510 65cf1e80 2018-03-16 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
511 65cf1e80 2018-03-16 stsp
512 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
513 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
514 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
515 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
516 65cf1e80 2018-03-16 stsp
517 65cf1e80 2018-03-16 stsp return err;
518 7e656b93 2018-03-17 stsp }
519 7e656b93 2018-03-17 stsp
520 7e656b93 2018-03-17 stsp static const struct got_error *
521 7e656b93 2018-03-17 stsp open_packfile(FILE **packfile, const char *path_packfile,
522 7e656b93 2018-03-17 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
523 7e656b93 2018-03-17 stsp {
524 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
525 7e656b93 2018-03-17 stsp
526 7e656b93 2018-03-17 stsp *packfile = NULL;
527 7e656b93 2018-03-17 stsp
528 7e656b93 2018-03-17 stsp *packfile = fopen(path_packfile, "rb");
529 7e656b93 2018-03-17 stsp if (*packfile == NULL) {
530 7e656b93 2018-03-17 stsp err = got_error_from_errno();
531 7e656b93 2018-03-17 stsp return err;
532 7e656b93 2018-03-17 stsp }
533 7e656b93 2018-03-17 stsp
534 7e656b93 2018-03-17 stsp if (packidx) {
535 7e656b93 2018-03-17 stsp err = read_packfile_hdr(*packfile, packidx);
536 7e656b93 2018-03-17 stsp if (err) {
537 7e656b93 2018-03-17 stsp fclose(*packfile);
538 7e656b93 2018-03-17 stsp *packfile = NULL;
539 7e656b93 2018-03-17 stsp }
540 7e656b93 2018-03-17 stsp }
541 7e656b93 2018-03-17 stsp return err;
542 7e656b93 2018-03-17 stsp }
543 7e656b93 2018-03-17 stsp
544 4589e373 2018-03-17 stsp void
545 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
546 7e656b93 2018-03-17 stsp {
547 4589e373 2018-03-17 stsp fclose(pack->packfile);
548 4589e373 2018-03-17 stsp pack->packfile = NULL;
549 4589e373 2018-03-17 stsp free(pack->path_packfile);
550 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
551 4589e373 2018-03-17 stsp pack->filesize = 0;
552 7e656b93 2018-03-17 stsp }
553 7e656b93 2018-03-17 stsp
554 7e656b93 2018-03-17 stsp static const struct got_error *
555 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
556 7e656b93 2018-03-17 stsp struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
557 7e656b93 2018-03-17 stsp {
558 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
559 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
560 7e656b93 2018-03-17 stsp int i;
561 7e656b93 2018-03-17 stsp
562 7e656b93 2018-03-17 stsp if (packp)
563 7e656b93 2018-03-17 stsp *packp = NULL;
564 7e656b93 2018-03-17 stsp
565 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
566 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
567 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
568 7e656b93 2018-03-17 stsp break;
569 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
570 7e656b93 2018-03-17 stsp return NULL;
571 c7fe698a 2018-01-23 stsp }
572 7e656b93 2018-03-17 stsp
573 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
574 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i - 1]);
575 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
576 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
577 7e656b93 2018-03-17 stsp i = 0;
578 7e656b93 2018-03-17 stsp }
579 7e656b93 2018-03-17 stsp
580 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
581 7e656b93 2018-03-17 stsp
582 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
583 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
584 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
585 7e656b93 2018-03-17 stsp goto done;
586 7e656b93 2018-03-17 stsp }
587 7e656b93 2018-03-17 stsp
588 7e656b93 2018-03-17 stsp err = open_packfile(&pack->packfile, path_packfile, repo, packidx);
589 7e656b93 2018-03-17 stsp if (err)
590 7e656b93 2018-03-17 stsp goto done;
591 7e656b93 2018-03-17 stsp
592 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
593 7e656b93 2018-03-17 stsp done:
594 7e656b93 2018-03-17 stsp if (err) {
595 7e656b93 2018-03-17 stsp if (pack)
596 7e656b93 2018-03-17 stsp free(pack->path_packfile);
597 7e656b93 2018-03-17 stsp free(pack);
598 7e656b93 2018-03-17 stsp } else if (packp)
599 7e656b93 2018-03-17 stsp *packp = pack;
600 c7fe698a 2018-01-23 stsp return err;
601 c7fe698a 2018-01-23 stsp }
602 c7fe698a 2018-01-23 stsp
603 7e656b93 2018-03-17 stsp struct got_pack *
604 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
605 7e656b93 2018-03-17 stsp {
606 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
607 7e656b93 2018-03-17 stsp int i;
608 7e656b93 2018-03-17 stsp
609 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
610 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
611 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
612 7e656b93 2018-03-17 stsp break;
613 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
614 7e656b93 2018-03-17 stsp return pack;
615 7e656b93 2018-03-17 stsp }
616 7e656b93 2018-03-17 stsp
617 7e656b93 2018-03-17 stsp return NULL;
618 7e656b93 2018-03-17 stsp }
619 7e656b93 2018-03-17 stsp
620 c7fe698a 2018-01-23 stsp static const struct got_error *
621 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
622 c3703302 2018-01-23 stsp FILE *packfile)
623 a487c1d0 2018-01-14 stsp {
624 a487c1d0 2018-01-14 stsp uint8_t t = 0;
625 a487c1d0 2018-01-14 stsp uint64_t s = 0;
626 a487c1d0 2018-01-14 stsp uint8_t sizeN;
627 a487c1d0 2018-01-14 stsp size_t n;
628 a487c1d0 2018-01-14 stsp int i = 0;
629 a487c1d0 2018-01-14 stsp
630 a1fd68d8 2018-01-12 stsp do {
631 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
632 a487c1d0 2018-01-14 stsp if (i > 9)
633 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
634 a1fd68d8 2018-01-12 stsp
635 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
636 a487c1d0 2018-01-14 stsp if (n != 1)
637 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
638 8251fdbc 2018-01-12 stsp
639 a1fd68d8 2018-01-12 stsp if (i == 0) {
640 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
641 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
642 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
643 a1fd68d8 2018-01-12 stsp } else {
644 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
645 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
646 a1fd68d8 2018-01-12 stsp }
647 a1fd68d8 2018-01-12 stsp i++;
648 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
649 a1fd68d8 2018-01-12 stsp
650 a487c1d0 2018-01-14 stsp *type = t;
651 a487c1d0 2018-01-14 stsp *size = s;
652 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
653 a487c1d0 2018-01-14 stsp return NULL;
654 0a0a3048 2018-01-10 stsp }
655 c54542a0 2018-01-13 stsp
656 a1fd68d8 2018-01-12 stsp static const struct got_error *
657 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
658 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
659 6ccb713b 2018-01-19 stsp {
660 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
661 6ccb713b 2018-01-19 stsp if (*obj == NULL)
662 0a585a0d 2018-03-17 stsp return got_error_from_errno();
663 6ccb713b 2018-01-19 stsp
664 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
665 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
666 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
667 6ccb713b 2018-01-19 stsp free(*obj);
668 6ccb713b 2018-01-19 stsp *obj = NULL;
669 0a585a0d 2018-03-17 stsp return err;
670 6ccb713b 2018-01-19 stsp }
671 6ccb713b 2018-01-19 stsp
672 6ccb713b 2018-01-19 stsp (*obj)->type = type;
673 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
674 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
675 6ccb713b 2018-01-19 stsp (*obj)->size = size;
676 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
677 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
678 b107e67f 2018-01-19 stsp
679 b107e67f 2018-01-19 stsp return NULL;
680 b107e67f 2018-01-19 stsp }
681 b107e67f 2018-01-19 stsp
682 b107e67f 2018-01-19 stsp static const struct got_error *
683 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
684 b107e67f 2018-01-19 stsp {
685 b107e67f 2018-01-19 stsp int64_t o = 0;
686 b107e67f 2018-01-19 stsp uint8_t offN;
687 b107e67f 2018-01-19 stsp size_t n;
688 b107e67f 2018-01-19 stsp int i = 0;
689 b107e67f 2018-01-19 stsp
690 b107e67f 2018-01-19 stsp do {
691 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
692 b107e67f 2018-01-19 stsp if (i > 8)
693 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
694 b107e67f 2018-01-19 stsp
695 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
696 b107e67f 2018-01-19 stsp if (n != 1)
697 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
698 b107e67f 2018-01-19 stsp
699 b107e67f 2018-01-19 stsp if (i == 0)
700 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
701 b107e67f 2018-01-19 stsp else {
702 cecc778e 2018-01-23 stsp o++;
703 b107e67f 2018-01-19 stsp o <<= 7;
704 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
705 b107e67f 2018-01-19 stsp }
706 b107e67f 2018-01-19 stsp i++;
707 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
708 6ccb713b 2018-01-19 stsp
709 b107e67f 2018-01-19 stsp *offset = o;
710 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
711 6ccb713b 2018-01-19 stsp return NULL;
712 6ccb713b 2018-01-19 stsp }
713 6ccb713b 2018-01-19 stsp
714 6ccb713b 2018-01-19 stsp static const struct got_error *
715 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
716 b107e67f 2018-01-19 stsp {
717 96f5e8b3 2018-01-23 stsp const struct got_error *err;
718 b107e67f 2018-01-19 stsp int64_t negoffset;
719 b107e67f 2018-01-19 stsp size_t negofflen;
720 b107e67f 2018-01-19 stsp
721 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
722 b107e67f 2018-01-19 stsp if (err)
723 b107e67f 2018-01-19 stsp return err;
724 b107e67f 2018-01-19 stsp
725 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
726 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
727 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
728 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
729 b107e67f 2018-01-19 stsp
730 96f5e8b3 2018-01-23 stsp return NULL;
731 96f5e8b3 2018-01-23 stsp }
732 96f5e8b3 2018-01-23 stsp
733 0e22967e 2018-02-11 stsp static const struct got_error *
734 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
735 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
736 a3500804 2018-01-23 stsp
737 96f5e8b3 2018-01-23 stsp static const struct got_error *
738 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
739 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
740 a53d2f13 2018-03-17 stsp size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
741 bdd2fbb3 2018-02-11 stsp {
742 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
743 bdd2fbb3 2018-02-11 stsp
744 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
745 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf,
746 a53d2f13 2018-03-17 stsp delta_len);
747 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
748 0a585a0d 2018-03-17 stsp return got_error_from_errno();
749 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
750 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
751 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
752 bdd2fbb3 2018-02-11 stsp return NULL;
753 bdd2fbb3 2018-02-11 stsp }
754 bdd2fbb3 2018-02-11 stsp
755 bdd2fbb3 2018-02-11 stsp static const struct got_error *
756 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
757 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
758 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
759 bdd2fbb3 2018-02-11 stsp
760 a3500804 2018-01-23 stsp {
761 a3500804 2018-01-23 stsp const struct got_error *err;
762 c3703302 2018-01-23 stsp off_t base_offset;
763 c3703302 2018-01-23 stsp uint8_t base_type;
764 c3703302 2018-01-23 stsp uint64_t base_size;
765 c3703302 2018-01-23 stsp size_t base_tslen;
766 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
767 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
768 a53d2f13 2018-03-17 stsp size_t delta_len;
769 a3500804 2018-01-23 stsp
770 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
771 a3500804 2018-01-23 stsp if (err)
772 a3500804 2018-01-23 stsp return err;
773 a3500804 2018-01-23 stsp
774 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
775 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
776 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
777 a53d2f13 2018-03-17 stsp
778 a53d2f13 2018-03-17 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, packfile);
779 a53d2f13 2018-03-17 stsp if (err)
780 a53d2f13 2018-03-17 stsp return err;
781 bdd2fbb3 2018-02-11 stsp
782 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
783 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
784 bdd2fbb3 2018-02-11 stsp if (err)
785 bdd2fbb3 2018-02-11 stsp return err;
786 bdd2fbb3 2018-02-11 stsp
787 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
788 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
789 b107e67f 2018-01-19 stsp return got_error_from_errno();
790 b107e67f 2018-01-19 stsp
791 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
792 b107e67f 2018-01-19 stsp packfile);
793 b107e67f 2018-01-19 stsp if (err)
794 b107e67f 2018-01-19 stsp return err;
795 b107e67f 2018-01-19 stsp
796 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
797 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
798 c3703302 2018-01-23 stsp }
799 c3703302 2018-01-23 stsp
800 c3703302 2018-01-23 stsp static const struct got_error *
801 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
802 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
803 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
804 c3703302 2018-01-23 stsp {
805 6b9c9673 2018-01-23 stsp const struct got_error *err;
806 6b9c9673 2018-01-23 stsp struct got_object_id id;
807 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
808 6b9c9673 2018-01-23 stsp int idx;
809 6b9c9673 2018-01-23 stsp off_t base_offset;
810 6b9c9673 2018-01-23 stsp uint8_t base_type;
811 6b9c9673 2018-01-23 stsp uint64_t base_size;
812 6b9c9673 2018-01-23 stsp size_t base_tslen;
813 6b9c9673 2018-01-23 stsp size_t n;
814 65cf1e80 2018-03-16 stsp char *path_base_packfile;
815 999f19f6 2018-03-17 stsp struct got_pack *base_pack;
816 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
817 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
818 a53d2f13 2018-03-17 stsp size_t delta_len;
819 6b9c9673 2018-01-23 stsp
820 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
821 6b9c9673 2018-01-23 stsp if (n != 1)
822 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
823 6b9c9673 2018-01-23 stsp
824 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
825 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
826 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
827 bdd2fbb3 2018-02-11 stsp
828 a53d2f13 2018-03-17 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, packfile);
829 a53d2f13 2018-03-17 stsp if (err)
830 a53d2f13 2018-03-17 stsp return err;
831 a53d2f13 2018-03-17 stsp
832 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
833 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
834 bdd2fbb3 2018-02-11 stsp if (err)
835 bdd2fbb3 2018-02-11 stsp return err;
836 bdd2fbb3 2018-02-11 stsp
837 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
838 6b9c9673 2018-01-23 stsp if (err)
839 6b9c9673 2018-01-23 stsp return err;
840 6b9c9673 2018-01-23 stsp
841 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
842 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
843 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
844 6b9c9673 2018-01-23 stsp }
845 6b9c9673 2018-01-23 stsp
846 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_base_packfile, repo, packidx);
847 7e656b93 2018-03-17 stsp if (err)
848 7e656b93 2018-03-17 stsp return err;
849 7e656b93 2018-03-17 stsp
850 999f19f6 2018-03-17 stsp base_pack = get_cached_pack(path_base_packfile, repo);
851 999f19f6 2018-03-17 stsp if (base_pack == NULL) {
852 999f19f6 2018-03-17 stsp err = cache_pack(&base_pack, path_base_packfile, NULL, repo);
853 999f19f6 2018-03-17 stsp if (err)
854 999f19f6 2018-03-17 stsp goto done;
855 999f19f6 2018-03-17 stsp }
856 6b9c9673 2018-01-23 stsp
857 999f19f6 2018-03-17 stsp if (fseeko(base_pack->packfile, base_offset, SEEK_SET) != 0) {
858 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
859 6b9c9673 2018-01-23 stsp goto done;
860 6b9c9673 2018-01-23 stsp }
861 6b9c9673 2018-01-23 stsp
862 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
863 999f19f6 2018-03-17 stsp base_pack->packfile);
864 6b9c9673 2018-01-23 stsp if (err)
865 6b9c9673 2018-01-23 stsp goto done;
866 6b9c9673 2018-01-23 stsp
867 999f19f6 2018-03-17 stsp err = resolve_delta_chain(deltas, repo, base_pack->packfile,
868 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
869 0e22967e 2018-02-11 stsp base_size);
870 6b9c9673 2018-01-23 stsp done:
871 65cf1e80 2018-03-16 stsp free(path_base_packfile);
872 6b9c9673 2018-01-23 stsp return err;
873 6b9c9673 2018-01-23 stsp }
874 6b9c9673 2018-01-23 stsp
875 6b9c9673 2018-01-23 stsp static const struct got_error *
876 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
877 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
878 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
879 6b9c9673 2018-01-23 stsp {
880 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
881 c3703302 2018-01-23 stsp
882 c3703302 2018-01-23 stsp switch (delta_type) {
883 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
884 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
885 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
886 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
887 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
888 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
889 a53d2f13 2018-03-17 stsp delta_type, delta_size, 0, NULL, 0);
890 4e8cda55 2018-01-19 stsp break;
891 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
892 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
893 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
894 bdd2fbb3 2018-02-11 stsp delta_size);
895 96f5e8b3 2018-01-23 stsp break;
896 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
897 bdd2fbb3 2018-02-11 stsp err = resolve_ref_delta(deltas, repo, packfile,
898 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
899 bdd2fbb3 2018-02-11 stsp delta_size);
900 6b9c9673 2018-01-23 stsp break;
901 4e8cda55 2018-01-19 stsp default:
902 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
903 4e8cda55 2018-01-19 stsp }
904 96f5e8b3 2018-01-23 stsp
905 96f5e8b3 2018-01-23 stsp return err;
906 96f5e8b3 2018-01-23 stsp }
907 96f5e8b3 2018-01-23 stsp
908 96f5e8b3 2018-01-23 stsp static const struct got_error *
909 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
910 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
911 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
912 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
913 96f5e8b3 2018-01-23 stsp {
914 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
915 96f5e8b3 2018-01-23 stsp int resolved_type;
916 4e8cda55 2018-01-19 stsp
917 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
918 b107e67f 2018-01-19 stsp if (*obj == NULL)
919 0a585a0d 2018-03-17 stsp return got_error_from_errno();
920 b107e67f 2018-01-19 stsp
921 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
922 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
923 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
924 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
925 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
926 b107e67f 2018-01-19 stsp
927 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
928 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
929 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
930 96f5e8b3 2018-01-23 stsp goto done;
931 96f5e8b3 2018-01-23 stsp }
932 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
933 96f5e8b3 2018-01-23 stsp
934 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
935 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
936 c3703302 2018-01-23 stsp
937 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
938 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
939 96f5e8b3 2018-01-23 stsp if (err)
940 96f5e8b3 2018-01-23 stsp goto done;
941 96f5e8b3 2018-01-23 stsp
942 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
943 96f5e8b3 2018-01-23 stsp if (err)
944 96f5e8b3 2018-01-23 stsp goto done;
945 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
946 96f5e8b3 2018-01-23 stsp
947 96f5e8b3 2018-01-23 stsp done:
948 96f5e8b3 2018-01-23 stsp if (err) {
949 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
950 96f5e8b3 2018-01-23 stsp *obj = NULL;
951 96f5e8b3 2018-01-23 stsp }
952 96f5e8b3 2018-01-23 stsp return err;
953 b107e67f 2018-01-19 stsp }
954 b107e67f 2018-01-19 stsp
955 b107e67f 2018-01-19 stsp static const struct got_error *
956 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
957 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
958 a1fd68d8 2018-01-12 stsp {
959 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
960 a1fd68d8 2018-01-12 stsp off_t offset;
961 65cf1e80 2018-03-16 stsp char *path_packfile;
962 6d89869a 2018-03-17 stsp struct got_pack *pack;
963 6c00b545 2018-01-17 stsp uint8_t type;
964 6c00b545 2018-01-17 stsp uint64_t size;
965 3ee5fc21 2018-01-17 stsp size_t tslen;
966 a1fd68d8 2018-01-12 stsp
967 6c00b545 2018-01-17 stsp *obj = NULL;
968 a1fd68d8 2018-01-12 stsp
969 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
970 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
971 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
972 a1fd68d8 2018-01-12 stsp
973 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_packfile, repo, packidx);
974 6b9c9673 2018-01-23 stsp if (err)
975 6b9c9673 2018-01-23 stsp return err;
976 a1fd68d8 2018-01-12 stsp
977 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
978 6d89869a 2018-03-17 stsp if (pack == NULL) {
979 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
980 6d89869a 2018-03-17 stsp if (err)
981 6d89869a 2018-03-17 stsp goto done;
982 6d89869a 2018-03-17 stsp }
983 7e656b93 2018-03-17 stsp
984 6d89869a 2018-03-17 stsp if (fseeko(pack->packfile, offset, SEEK_SET) != 0) {
985 6c00b545 2018-01-17 stsp err = got_error_from_errno();
986 6c00b545 2018-01-17 stsp goto done;
987 6c00b545 2018-01-17 stsp }
988 6c00b545 2018-01-17 stsp
989 6d89869a 2018-03-17 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack->packfile);
990 a1fd68d8 2018-01-12 stsp if (err)
991 a1fd68d8 2018-01-12 stsp goto done;
992 a1fd68d8 2018-01-12 stsp
993 6c00b545 2018-01-17 stsp switch (type) {
994 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
995 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
996 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
997 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
998 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
999 6ccb713b 2018-01-19 stsp offset + tslen, size);
1000 6c00b545 2018-01-17 stsp break;
1001 6ccb713b 2018-01-19 stsp
1002 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
1003 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
1004 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
1005 6d89869a 2018-03-17 stsp pack->packfile, id, offset, tslen, type, size);
1006 b107e67f 2018-01-19 stsp break;
1007 b107e67f 2018-01-19 stsp
1008 6c00b545 2018-01-17 stsp default:
1009 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
1010 6c00b545 2018-01-17 stsp goto done;
1011 6c00b545 2018-01-17 stsp }
1012 a1fd68d8 2018-01-12 stsp done:
1013 65cf1e80 2018-03-16 stsp free(path_packfile);
1014 a1fd68d8 2018-01-12 stsp return err;
1015 a1fd68d8 2018-01-12 stsp }
1016 a1fd68d8 2018-01-12 stsp
1017 a1fd68d8 2018-01-12 stsp const struct got_error *
1018 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1019 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1020 a1fd68d8 2018-01-12 stsp {
1021 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1022 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
1023 6b9c9673 2018-01-23 stsp int idx;
1024 a1fd68d8 2018-01-12 stsp
1025 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1026 6b9c9673 2018-01-23 stsp if (err)
1027 6b9c9673 2018-01-23 stsp return err;
1028 a1fd68d8 2018-01-12 stsp
1029 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1030 7e656b93 2018-03-17 stsp if (err)
1031 7e656b93 2018-03-17 stsp return err;
1032 7e656b93 2018-03-17 stsp
1033 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1034 3ee5fc21 2018-01-17 stsp return err;
1035 3ee5fc21 2018-01-17 stsp }
1036 efd2a263 2018-01-19 stsp
1037 efd2a263 2018-01-19 stsp static const struct got_error *
1038 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1039 22484865 2018-03-13 stsp {
1040 22484865 2018-03-13 stsp struct got_delta *delta;
1041 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1042 22484865 2018-03-13 stsp
1043 22484865 2018-03-13 stsp *max_size = 0;
1044 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1045 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1046 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1047 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1048 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1049 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1050 22484865 2018-03-13 stsp const struct got_error *err;
1051 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1052 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1053 22484865 2018-03-13 stsp if (err)
1054 22484865 2018-03-13 stsp return err;
1055 22484865 2018-03-13 stsp } else
1056 22484865 2018-03-13 stsp base_size = delta->size;
1057 22484865 2018-03-13 stsp if (base_size > *max_size)
1058 22484865 2018-03-13 stsp *max_size = base_size;
1059 22484865 2018-03-13 stsp if (result_size > *max_size)
1060 22484865 2018-03-13 stsp *max_size = result_size;
1061 22484865 2018-03-13 stsp }
1062 bd1223b9 2018-03-14 stsp
1063 9feb4ff2 2018-03-14 stsp return NULL;
1064 22484865 2018-03-13 stsp }
1065 22484865 2018-03-13 stsp
1066 22484865 2018-03-13 stsp static const struct got_error *
1067 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1068 72eb3431 2018-04-01 stsp FILE *outfile, struct got_repository *repo)
1069 efd2a263 2018-01-19 stsp {
1070 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1071 6691714a 2018-01-23 stsp struct got_delta *delta;
1072 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1073 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1074 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1075 22484865 2018-03-13 stsp uint64_t max_size;
1076 6691714a 2018-01-23 stsp int n = 0;
1077 b29656e2 2018-03-16 stsp
1078 b29656e2 2018-03-16 stsp *result_size = 0;
1079 3ee5fc21 2018-01-17 stsp
1080 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1081 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1082 efd2a263 2018-01-19 stsp
1083 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1084 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1085 22484865 2018-03-13 stsp if (err)
1086 22484865 2018-03-13 stsp return err;
1087 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1088 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1089 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1090 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1091 8628c62d 2018-03-15 stsp } else {
1092 22484865 2018-03-13 stsp base_file = got_opentemp();
1093 8628c62d 2018-03-15 stsp if (base_file == NULL)
1094 8628c62d 2018-03-15 stsp return got_error_from_errno();
1095 efd2a263 2018-01-19 stsp
1096 22484865 2018-03-13 stsp accum_file = got_opentemp();
1097 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1098 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1099 8628c62d 2018-03-15 stsp fclose(base_file);
1100 8628c62d 2018-03-15 stsp return err;
1101 8628c62d 2018-03-15 stsp }
1102 6691714a 2018-01-23 stsp }
1103 efd2a263 2018-01-19 stsp
1104 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1105 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1106 a6b158cc 2018-02-11 stsp if (n == 0) {
1107 72eb3431 2018-04-01 stsp struct got_pack *pack;
1108 8628c62d 2018-03-15 stsp size_t base_len;
1109 9db65d41 2018-03-14 stsp
1110 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1111 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1112 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1113 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1114 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1115 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1116 72eb3431 2018-04-01 stsp goto done;
1117 72eb3431 2018-04-01 stsp }
1118 72eb3431 2018-04-01 stsp
1119 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1120 72eb3431 2018-04-01 stsp if (pack == NULL) {
1121 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1122 a6b158cc 2018-02-11 stsp goto done;
1123 a6b158cc 2018-02-11 stsp }
1124 6691714a 2018-01-23 stsp
1125 f7e127f3 2018-03-17 stsp if (fseeko(pack->packfile, delta->offset + delta->tslen,
1126 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
1127 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
1128 bdd2fbb3 2018-02-11 stsp goto done;
1129 bdd2fbb3 2018-02-11 stsp }
1130 8628c62d 2018-03-15 stsp if (base_file)
1131 7e656b93 2018-03-17 stsp err = got_inflate_to_file(&base_len,
1132 f7e127f3 2018-03-17 stsp pack->packfile, base_file);
1133 8628c62d 2018-03-15 stsp else {
1134 8628c62d 2018-03-15 stsp err = got_inflate_to_mem(&base_buf, &base_len,
1135 f7e127f3 2018-03-17 stsp pack->packfile);
1136 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1137 8628c62d 2018-03-15 stsp uint8_t *p;
1138 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1139 8628c62d 2018-03-15 stsp if (p == NULL) {
1140 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1141 8628c62d 2018-03-15 stsp goto done;
1142 8628c62d 2018-03-15 stsp }
1143 8628c62d 2018-03-15 stsp base_buf = p;
1144 8628c62d 2018-03-15 stsp }
1145 8628c62d 2018-03-15 stsp }
1146 a6b158cc 2018-02-11 stsp if (err)
1147 a6b158cc 2018-02-11 stsp goto done;
1148 a6b158cc 2018-02-11 stsp n++;
1149 8628c62d 2018-03-15 stsp if (base_file)
1150 8628c62d 2018-03-15 stsp rewind(base_file);
1151 a6b158cc 2018-02-11 stsp continue;
1152 9db65d41 2018-03-14 stsp }
1153 885d3e02 2018-01-27 stsp
1154 8628c62d 2018-03-15 stsp if (base_buf) {
1155 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1156 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1157 8628c62d 2018-03-15 stsp n++;
1158 8628c62d 2018-03-15 stsp } else {
1159 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1160 a53d2f13 2018-03-17 stsp delta->delta_len,
1161 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1162 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1163 b29656e2 2018-03-16 stsp &accum_size);
1164 8628c62d 2018-03-15 stsp }
1165 6691714a 2018-01-23 stsp if (err)
1166 6691714a 2018-01-23 stsp goto done;
1167 6691714a 2018-01-23 stsp
1168 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1169 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1170 8628c62d 2018-03-15 stsp if (base_buf) {
1171 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1172 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1173 8628c62d 2018-03-15 stsp base_buf = tmp;
1174 8628c62d 2018-03-15 stsp } else {
1175 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1176 8628c62d 2018-03-15 stsp accum_file = base_file;
1177 8628c62d 2018-03-15 stsp base_file = tmp;
1178 8628c62d 2018-03-15 stsp rewind(base_file);
1179 8628c62d 2018-03-15 stsp rewind(accum_file);
1180 8628c62d 2018-03-15 stsp }
1181 6691714a 2018-01-23 stsp }
1182 6691714a 2018-01-23 stsp }
1183 6691714a 2018-01-23 stsp
1184 6691714a 2018-01-23 stsp done:
1185 8628c62d 2018-03-15 stsp free(base_buf);
1186 8628c62d 2018-03-15 stsp if (accum_buf) {
1187 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1188 8628c62d 2018-03-15 stsp free(accum_buf);
1189 8628c62d 2018-03-15 stsp if (len != accum_size)
1190 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1191 8628c62d 2018-03-15 stsp }
1192 8628c62d 2018-03-15 stsp if (base_file)
1193 8628c62d 2018-03-15 stsp fclose(base_file);
1194 8628c62d 2018-03-15 stsp if (accum_file)
1195 8628c62d 2018-03-15 stsp fclose(accum_file);
1196 6691714a 2018-01-23 stsp rewind(outfile);
1197 b29656e2 2018-03-16 stsp if (err == NULL)
1198 b29656e2 2018-03-16 stsp *result_size = accum_size;
1199 e0ab43e7 2018-03-16 stsp return err;
1200 e0ab43e7 2018-03-16 stsp }
1201 e0ab43e7 2018-03-16 stsp
1202 e0ab43e7 2018-03-16 stsp static const struct got_error *
1203 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1204 72eb3431 2018-04-01 stsp struct got_delta_chain *deltas, struct got_repository *repo)
1205 e0ab43e7 2018-03-16 stsp {
1206 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1207 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1208 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1209 e0ab43e7 2018-03-16 stsp size_t accum_size;
1210 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1211 e0ab43e7 2018-03-16 stsp int n = 0;
1212 e0ab43e7 2018-03-16 stsp
1213 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1214 e0ab43e7 2018-03-16 stsp *outlen = 0;
1215 e0ab43e7 2018-03-16 stsp
1216 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1217 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1218 e0ab43e7 2018-03-16 stsp
1219 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1220 e0ab43e7 2018-03-16 stsp if (err)
1221 e0ab43e7 2018-03-16 stsp return err;
1222 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1223 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1224 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1225 e0ab43e7 2018-03-16 stsp
1226 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1227 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1228 e0ab43e7 2018-03-16 stsp if (n == 0) {
1229 72eb3431 2018-04-01 stsp struct got_pack *pack;
1230 e0ab43e7 2018-03-16 stsp size_t base_len;
1231 e0ab43e7 2018-03-16 stsp
1232 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1233 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1234 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1235 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1236 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1237 72eb3431 2018-04-01 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1238 72eb3431 2018-04-01 stsp goto done;
1239 72eb3431 2018-04-01 stsp }
1240 72eb3431 2018-04-01 stsp
1241 72eb3431 2018-04-01 stsp pack = get_cached_pack(delta->path_packfile, repo);
1242 72eb3431 2018-04-01 stsp if (pack == NULL) {
1243 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1244 e0ab43e7 2018-03-16 stsp goto done;
1245 e0ab43e7 2018-03-16 stsp }
1246 e0ab43e7 2018-03-16 stsp
1247 f7e127f3 2018-03-17 stsp if (fseeko(pack->packfile, delta->offset + delta->tslen,
1248 e0ab43e7 2018-03-16 stsp SEEK_SET) != 0) {
1249 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1250 e0ab43e7 2018-03-16 stsp goto done;
1251 e0ab43e7 2018-03-16 stsp }
1252 e0ab43e7 2018-03-16 stsp err = got_inflate_to_mem(&base_buf, &base_len,
1253 f7e127f3 2018-03-17 stsp pack->packfile);
1254 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1255 e0ab43e7 2018-03-16 stsp uint8_t *p;
1256 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1257 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1258 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1259 e0ab43e7 2018-03-16 stsp goto done;
1260 e0ab43e7 2018-03-16 stsp }
1261 e0ab43e7 2018-03-16 stsp base_buf = p;
1262 e0ab43e7 2018-03-16 stsp }
1263 e0ab43e7 2018-03-16 stsp if (err)
1264 e0ab43e7 2018-03-16 stsp goto done;
1265 e0ab43e7 2018-03-16 stsp n++;
1266 e0ab43e7 2018-03-16 stsp continue;
1267 e0ab43e7 2018-03-16 stsp }
1268 e0ab43e7 2018-03-16 stsp
1269 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1270 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1271 e0ab43e7 2018-03-16 stsp n++;
1272 e0ab43e7 2018-03-16 stsp if (err)
1273 e0ab43e7 2018-03-16 stsp goto done;
1274 e0ab43e7 2018-03-16 stsp
1275 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1276 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1277 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1278 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1279 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1280 e0ab43e7 2018-03-16 stsp }
1281 e0ab43e7 2018-03-16 stsp }
1282 e0ab43e7 2018-03-16 stsp
1283 e0ab43e7 2018-03-16 stsp done:
1284 e0ab43e7 2018-03-16 stsp free(base_buf);
1285 e0ab43e7 2018-03-16 stsp if (err) {
1286 e0ab43e7 2018-03-16 stsp free(accum_buf);
1287 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1288 e0ab43e7 2018-03-16 stsp *outlen = 0;
1289 e0ab43e7 2018-03-16 stsp } else {
1290 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1291 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1292 e0ab43e7 2018-03-16 stsp }
1293 efd2a263 2018-01-19 stsp return err;
1294 efd2a263 2018-01-19 stsp }
1295 efd2a263 2018-01-19 stsp
1296 3ee5fc21 2018-01-17 stsp const struct got_error *
1297 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1298 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1299 3ee5fc21 2018-01-17 stsp {
1300 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1301 3ee5fc21 2018-01-17 stsp
1302 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1303 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1304 3ee5fc21 2018-01-17 stsp
1305 040bf4a1 2018-04-01 stsp *f = got_opentemp();
1306 040bf4a1 2018-04-01 stsp if (*f == NULL) {
1307 040bf4a1 2018-04-01 stsp err = got_error(GOT_ERR_FILE_OPEN);
1308 040bf4a1 2018-04-01 stsp goto done;
1309 ef2bccd9 2018-03-16 stsp }
1310 3ee5fc21 2018-01-17 stsp
1311 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1312 72eb3431 2018-04-01 stsp struct got_pack *pack;
1313 72eb3431 2018-04-01 stsp
1314 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1315 72eb3431 2018-04-01 stsp if (pack == NULL) {
1316 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1317 72eb3431 2018-04-01 stsp if (err)
1318 72eb3431 2018-04-01 stsp goto done;
1319 72eb3431 2018-04-01 stsp }
1320 72eb3431 2018-04-01 stsp
1321 7e656b93 2018-03-17 stsp if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
1322 6691714a 2018-01-23 stsp err = got_error_from_errno();
1323 c52ac529 2018-03-17 stsp goto done;
1324 c52ac529 2018-03-17 stsp }
1325 72eb3431 2018-04-01 stsp
1326 7e656b93 2018-03-17 stsp err = got_inflate_to_file(&obj->size, pack->packfile, *f);
1327 040bf4a1 2018-04-01 stsp } else
1328 b29656e2 2018-03-16 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, *f,
1329 72eb3431 2018-04-01 stsp repo);
1330 3ee5fc21 2018-01-17 stsp done:
1331 d0f3be7c 2018-03-17 stsp if (err && *f) {
1332 3ee5fc21 2018-01-17 stsp fclose(*f);
1333 d0f3be7c 2018-03-17 stsp *f = NULL;
1334 d0f3be7c 2018-03-17 stsp }
1335 a1fd68d8 2018-01-12 stsp return err;
1336 a1fd68d8 2018-01-12 stsp }
1337 e0ab43e7 2018-03-16 stsp
1338 e0ab43e7 2018-03-16 stsp const struct got_error *
1339 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1340 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1341 e0ab43e7 2018-03-16 stsp {
1342 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1343 e0ab43e7 2018-03-16 stsp
1344 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1345 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1346 e0ab43e7 2018-03-16 stsp
1347 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1348 72eb3431 2018-04-01 stsp struct got_pack *pack;
1349 72eb3431 2018-04-01 stsp
1350 72eb3431 2018-04-01 stsp pack = get_cached_pack(obj->path_packfile, repo);
1351 72eb3431 2018-04-01 stsp if (pack == NULL) {
1352 72eb3431 2018-04-01 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1353 72eb3431 2018-04-01 stsp if (err)
1354 72eb3431 2018-04-01 stsp goto done;
1355 72eb3431 2018-04-01 stsp }
1356 72eb3431 2018-04-01 stsp
1357 7e656b93 2018-03-17 stsp if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
1358 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1359 e0ab43e7 2018-03-16 stsp goto done;
1360 e0ab43e7 2018-03-16 stsp }
1361 e0ab43e7 2018-03-16 stsp
1362 7e656b93 2018-03-17 stsp err = got_inflate_to_mem(buf, len, pack->packfile);
1363 e0ab43e7 2018-03-16 stsp } else
1364 72eb3431 2018-04-01 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, repo);
1365 e0ab43e7 2018-03-16 stsp done:
1366 e0ab43e7 2018-03-16 stsp return err;
1367 e0ab43e7 2018-03-16 stsp }