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 a1fd68d8 2018-01-12 stsp
294 a1fd68d8 2018-01-12 stsp static int
295 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
296 a1fd68d8 2018-01-12 stsp {
297 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
298 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
299 a1fd68d8 2018-01-12 stsp int i = 0;
300 a1fd68d8 2018-01-12 stsp
301 a1fd68d8 2018-01-12 stsp if (id0 > 0)
302 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
303 a1fd68d8 2018-01-12 stsp
304 a1fd68d8 2018-01-12 stsp while (i < totobj) {
305 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
306 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
307 a1fd68d8 2018-01-12 stsp
308 6c00b545 2018-01-17 stsp if (cmp == 0)
309 6c00b545 2018-01-17 stsp return i;
310 6c00b545 2018-01-17 stsp i++;
311 a1fd68d8 2018-01-12 stsp }
312 a1fd68d8 2018-01-12 stsp
313 a1fd68d8 2018-01-12 stsp return -1;
314 79b11c62 2018-03-09 stsp }
315 79b11c62 2018-03-09 stsp
316 79b11c62 2018-03-09 stsp static struct got_packidx_v2_hdr *
317 79b11c62 2018-03-09 stsp dup_packidx(struct got_packidx_v2_hdr *packidx)
318 79b11c62 2018-03-09 stsp {
319 79b11c62 2018-03-09 stsp struct got_packidx_v2_hdr *p;
320 79b11c62 2018-03-09 stsp size_t nobj;
321 79b11c62 2018-03-09 stsp
322 79b11c62 2018-03-09 stsp p = calloc(1, sizeof(*p));
323 79b11c62 2018-03-09 stsp if (p == NULL)
324 79b11c62 2018-03-09 stsp return NULL;
325 79b11c62 2018-03-09 stsp
326 79b11c62 2018-03-09 stsp memcpy(p, packidx, sizeof(*p));
327 79b11c62 2018-03-09 stsp p->sorted_ids = NULL;
328 79b11c62 2018-03-09 stsp p->crc32 = NULL;
329 79b11c62 2018-03-09 stsp p->offsets = NULL;
330 79b11c62 2018-03-09 stsp p->large_offsets = NULL;
331 79b11c62 2018-03-09 stsp
332 79b11c62 2018-03-09 stsp nobj = betoh32(p->fanout_table[0xff]);
333 79b11c62 2018-03-09 stsp
334 79b11c62 2018-03-09 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
335 79b11c62 2018-03-09 stsp if (p->sorted_ids == NULL)
336 79b11c62 2018-03-09 stsp goto err;
337 79b11c62 2018-03-09 stsp memcpy(p->sorted_ids, packidx->sorted_ids, nobj * sizeof(*p->sorted_ids));
338 79b11c62 2018-03-09 stsp
339 79b11c62 2018-03-09 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
340 79b11c62 2018-03-09 stsp if (p->crc32 == NULL)
341 79b11c62 2018-03-09 stsp goto err;
342 79b11c62 2018-03-09 stsp memcpy(p->crc32, packidx->crc32, nobj * sizeof(*p->crc32));
343 79b11c62 2018-03-09 stsp
344 79b11c62 2018-03-09 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
345 79b11c62 2018-03-09 stsp if (p->offsets == NULL)
346 79b11c62 2018-03-09 stsp goto err;
347 79b11c62 2018-03-09 stsp memcpy(p->offsets, packidx->offsets, nobj * sizeof(*p->offsets));
348 79b11c62 2018-03-09 stsp
349 79b11c62 2018-03-09 stsp if (p->large_offsets) {
350 79b11c62 2018-03-09 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
351 79b11c62 2018-03-09 stsp if (p->large_offsets == NULL)
352 79b11c62 2018-03-09 stsp goto err;
353 79b11c62 2018-03-09 stsp memcpy(p->large_offsets, packidx->large_offsets,
354 79b11c62 2018-03-09 stsp nobj * sizeof(*p->large_offsets));
355 79b11c62 2018-03-09 stsp }
356 79b11c62 2018-03-09 stsp
357 79b11c62 2018-03-09 stsp return p;
358 79b11c62 2018-03-09 stsp
359 79b11c62 2018-03-09 stsp err:
360 79b11c62 2018-03-09 stsp free(p->large_offsets);
361 79b11c62 2018-03-09 stsp free(p->offsets);
362 79b11c62 2018-03-09 stsp free(p->crc32);
363 79b11c62 2018-03-09 stsp free(p->sorted_ids);
364 79b11c62 2018-03-09 stsp free(p);
365 79b11c62 2018-03-09 stsp return NULL;
366 87c99799 2018-03-16 stsp }
367 87c99799 2018-03-16 stsp
368 65cf1e80 2018-03-16 stsp static void
369 65cf1e80 2018-03-16 stsp cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
370 87c99799 2018-03-16 stsp {
371 79b11c62 2018-03-09 stsp int i;
372 79b11c62 2018-03-09 stsp
373 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
374 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
375 79b11c62 2018-03-09 stsp break;
376 79b11c62 2018-03-09 stsp }
377 79b11c62 2018-03-09 stsp
378 65cf1e80 2018-03-16 stsp if (i == nitems(repo->packidx_cache)) {
379 65cf1e80 2018-03-16 stsp got_packidx_close(repo->packidx_cache[i - 1]);
380 65cf1e80 2018-03-16 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
381 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache) -
382 65cf1e80 2018-03-16 stsp sizeof(repo->packidx_cache[0]));
383 79b11c62 2018-03-09 stsp i = 0;
384 79b11c62 2018-03-09 stsp }
385 79b11c62 2018-03-09 stsp
386 65cf1e80 2018-03-16 stsp repo->packidx_cache[i] = dup_packidx(packidx);
387 79b11c62 2018-03-09 stsp }
388 79b11c62 2018-03-09 stsp
389 6b9c9673 2018-01-23 stsp static const struct got_error *
390 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
391 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
392 6b9c9673 2018-01-23 stsp {
393 6b9c9673 2018-01-23 stsp const struct got_error *err;
394 6b9c9673 2018-01-23 stsp char *path_packdir;
395 6b9c9673 2018-01-23 stsp DIR *packdir;
396 6b9c9673 2018-01-23 stsp struct dirent *dent;
397 6b9c9673 2018-01-23 stsp char *path_packidx;
398 79b11c62 2018-03-09 stsp int i;
399 6b9c9673 2018-01-23 stsp
400 65cf1e80 2018-03-16 stsp /* Search pack index cache. */
401 65cf1e80 2018-03-16 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
402 65cf1e80 2018-03-16 stsp if (repo->packidx_cache[i] == NULL)
403 79b11c62 2018-03-09 stsp break;
404 65cf1e80 2018-03-16 stsp *idx = get_object_idx(repo->packidx_cache[i], id);
405 79b11c62 2018-03-09 stsp if (*idx != -1) {
406 6bb255dc 2018-03-17 stsp *packidx = repo->packidx_cache[i];
407 79b11c62 2018-03-09 stsp return NULL;
408 79b11c62 2018-03-09 stsp }
409 79b11c62 2018-03-09 stsp }
410 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
411 79b11c62 2018-03-09 stsp
412 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
413 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
414 0a585a0d 2018-03-17 stsp return got_error_from_errno();
415 6b9c9673 2018-01-23 stsp
416 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
417 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
418 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
419 6b9c9673 2018-01-23 stsp goto done;
420 6b9c9673 2018-01-23 stsp }
421 6b9c9673 2018-01-23 stsp
422 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
423 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
424 6b9c9673 2018-01-23 stsp continue;
425 6b9c9673 2018-01-23 stsp
426 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
427 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
428 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
429 6b9c9673 2018-01-23 stsp goto done;
430 6b9c9673 2018-01-23 stsp }
431 6b9c9673 2018-01-23 stsp
432 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
433 6b9c9673 2018-01-23 stsp free(path_packidx);
434 6b9c9673 2018-01-23 stsp if (err)
435 6b9c9673 2018-01-23 stsp goto done;
436 6b9c9673 2018-01-23 stsp
437 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
438 6b9c9673 2018-01-23 stsp if (*idx != -1) {
439 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
440 65cf1e80 2018-03-16 stsp cache_packidx(*packidx, repo);
441 6b9c9673 2018-01-23 stsp goto done;
442 6b9c9673 2018-01-23 stsp }
443 6b9c9673 2018-01-23 stsp
444 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
445 6b9c9673 2018-01-23 stsp *packidx = NULL;
446 6b9c9673 2018-01-23 stsp }
447 6b9c9673 2018-01-23 stsp
448 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
449 6b9c9673 2018-01-23 stsp done:
450 6b9c9673 2018-01-23 stsp free(path_packdir);
451 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
452 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
453 6b9c9673 2018-01-23 stsp return err;
454 6b9c9673 2018-01-23 stsp }
455 6b9c9673 2018-01-23 stsp
456 c7fe698a 2018-01-23 stsp static const struct got_error *
457 65cf1e80 2018-03-16 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
458 65cf1e80 2018-03-16 stsp struct got_packidx_v2_hdr *packidx)
459 c7fe698a 2018-01-23 stsp {
460 65cf1e80 2018-03-16 stsp char *path_packdir;
461 65cf1e80 2018-03-16 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
462 65cf1e80 2018-03-16 stsp char *sha1str;
463 c7fe698a 2018-01-23 stsp
464 65cf1e80 2018-03-16 stsp path_packdir = got_repo_get_path_objects_pack(repo);
465 65cf1e80 2018-03-16 stsp if (path_packdir == NULL)
466 0a585a0d 2018-03-17 stsp return got_error_from_errno();
467 c7fe698a 2018-01-23 stsp
468 65cf1e80 2018-03-16 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
469 65cf1e80 2018-03-16 stsp hex, sizeof(hex));
470 65cf1e80 2018-03-16 stsp if (sha1str == NULL)
471 65cf1e80 2018-03-16 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
472 87c99799 2018-03-16 stsp
473 65cf1e80 2018-03-16 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
474 65cf1e80 2018-03-16 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
475 65cf1e80 2018-03-16 stsp *path_packfile = NULL;
476 0a585a0d 2018-03-17 stsp return got_error_from_errno();
477 87c99799 2018-03-16 stsp }
478 87c99799 2018-03-16 stsp
479 65cf1e80 2018-03-16 stsp return NULL;
480 65cf1e80 2018-03-16 stsp }
481 65cf1e80 2018-03-16 stsp
482 65cf1e80 2018-03-16 stsp const struct got_error *
483 65cf1e80 2018-03-16 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
484 65cf1e80 2018-03-16 stsp {
485 65cf1e80 2018-03-16 stsp const struct got_error *err = NULL;
486 65cf1e80 2018-03-16 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
487 65cf1e80 2018-03-16 stsp struct got_packfile_hdr hdr;
488 65cf1e80 2018-03-16 stsp size_t n;
489 65cf1e80 2018-03-16 stsp
490 65cf1e80 2018-03-16 stsp n = fread(&hdr, sizeof(hdr), 1, f);
491 65cf1e80 2018-03-16 stsp if (n != 1)
492 65cf1e80 2018-03-16 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
493 65cf1e80 2018-03-16 stsp
494 65cf1e80 2018-03-16 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
495 65cf1e80 2018-03-16 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
496 65cf1e80 2018-03-16 stsp betoh32(hdr.nobjects) != totobj)
497 65cf1e80 2018-03-16 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
498 65cf1e80 2018-03-16 stsp
499 65cf1e80 2018-03-16 stsp return err;
500 7e656b93 2018-03-17 stsp }
501 7e656b93 2018-03-17 stsp
502 7e656b93 2018-03-17 stsp static const struct got_error *
503 7e656b93 2018-03-17 stsp open_packfile(FILE **packfile, const char *path_packfile,
504 7e656b93 2018-03-17 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
505 7e656b93 2018-03-17 stsp {
506 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
507 7e656b93 2018-03-17 stsp
508 7e656b93 2018-03-17 stsp *packfile = NULL;
509 7e656b93 2018-03-17 stsp
510 7e656b93 2018-03-17 stsp *packfile = fopen(path_packfile, "rb");
511 7e656b93 2018-03-17 stsp if (*packfile == NULL) {
512 7e656b93 2018-03-17 stsp err = got_error_from_errno();
513 7e656b93 2018-03-17 stsp return err;
514 7e656b93 2018-03-17 stsp }
515 7e656b93 2018-03-17 stsp
516 7e656b93 2018-03-17 stsp if (packidx) {
517 7e656b93 2018-03-17 stsp err = read_packfile_hdr(*packfile, packidx);
518 7e656b93 2018-03-17 stsp if (err) {
519 7e656b93 2018-03-17 stsp fclose(*packfile);
520 7e656b93 2018-03-17 stsp *packfile = NULL;
521 7e656b93 2018-03-17 stsp }
522 7e656b93 2018-03-17 stsp }
523 7e656b93 2018-03-17 stsp return err;
524 7e656b93 2018-03-17 stsp }
525 7e656b93 2018-03-17 stsp
526 4589e373 2018-03-17 stsp void
527 7e656b93 2018-03-17 stsp got_pack_close(struct got_pack *pack)
528 7e656b93 2018-03-17 stsp {
529 4589e373 2018-03-17 stsp fclose(pack->packfile);
530 4589e373 2018-03-17 stsp pack->packfile = NULL;
531 4589e373 2018-03-17 stsp free(pack->path_packfile);
532 4589e373 2018-03-17 stsp pack->path_packfile = NULL;
533 4589e373 2018-03-17 stsp pack->filesize = 0;
534 7e656b93 2018-03-17 stsp }
535 7e656b93 2018-03-17 stsp
536 7e656b93 2018-03-17 stsp static const struct got_error *
537 7e656b93 2018-03-17 stsp cache_pack(struct got_pack **packp, const char *path_packfile,
538 7e656b93 2018-03-17 stsp struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
539 7e656b93 2018-03-17 stsp {
540 7e656b93 2018-03-17 stsp const struct got_error *err = NULL;
541 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
542 7e656b93 2018-03-17 stsp int i;
543 7e656b93 2018-03-17 stsp
544 7e656b93 2018-03-17 stsp if (packp)
545 7e656b93 2018-03-17 stsp *packp = NULL;
546 7e656b93 2018-03-17 stsp
547 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
548 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
549 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
550 7e656b93 2018-03-17 stsp break;
551 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
552 7e656b93 2018-03-17 stsp return NULL;
553 c7fe698a 2018-01-23 stsp }
554 7e656b93 2018-03-17 stsp
555 7e656b93 2018-03-17 stsp if (i == nitems(repo->packs) - 1) {
556 7e656b93 2018-03-17 stsp got_pack_close(&repo->packs[i - 1]);
557 7e656b93 2018-03-17 stsp memmove(&repo->packs[1], &repo->packs[0],
558 7e656b93 2018-03-17 stsp sizeof(repo->packs) - sizeof(repo->packs[0]));
559 7e656b93 2018-03-17 stsp i = 0;
560 7e656b93 2018-03-17 stsp }
561 7e656b93 2018-03-17 stsp
562 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
563 7e656b93 2018-03-17 stsp
564 7e656b93 2018-03-17 stsp pack->path_packfile = strdup(path_packfile);
565 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL) {
566 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
567 7e656b93 2018-03-17 stsp goto done;
568 7e656b93 2018-03-17 stsp }
569 7e656b93 2018-03-17 stsp
570 7e656b93 2018-03-17 stsp err = open_packfile(&pack->packfile, path_packfile, repo, packidx);
571 7e656b93 2018-03-17 stsp if (err)
572 7e656b93 2018-03-17 stsp goto done;
573 7e656b93 2018-03-17 stsp
574 7e656b93 2018-03-17 stsp err = get_packfile_size(&pack->filesize, path_packfile);
575 7e656b93 2018-03-17 stsp done:
576 7e656b93 2018-03-17 stsp if (err) {
577 7e656b93 2018-03-17 stsp if (pack)
578 7e656b93 2018-03-17 stsp free(pack->path_packfile);
579 7e656b93 2018-03-17 stsp free(pack);
580 7e656b93 2018-03-17 stsp } else if (packp)
581 7e656b93 2018-03-17 stsp *packp = pack;
582 c7fe698a 2018-01-23 stsp return err;
583 c7fe698a 2018-01-23 stsp }
584 c7fe698a 2018-01-23 stsp
585 7e656b93 2018-03-17 stsp struct got_pack *
586 7e656b93 2018-03-17 stsp get_cached_pack(const char *path_packfile, struct got_repository *repo)
587 7e656b93 2018-03-17 stsp {
588 7e656b93 2018-03-17 stsp struct got_pack *pack = NULL;
589 7e656b93 2018-03-17 stsp int i;
590 7e656b93 2018-03-17 stsp
591 7e656b93 2018-03-17 stsp for (i = 0; i < nitems(repo->packs); i++) {
592 7e656b93 2018-03-17 stsp pack = &repo->packs[i];
593 7e656b93 2018-03-17 stsp if (pack->path_packfile == NULL)
594 7e656b93 2018-03-17 stsp break;
595 7e656b93 2018-03-17 stsp if (strcmp(pack->path_packfile, path_packfile) == 0)
596 7e656b93 2018-03-17 stsp return pack;
597 7e656b93 2018-03-17 stsp }
598 7e656b93 2018-03-17 stsp
599 7e656b93 2018-03-17 stsp return NULL;
600 7e656b93 2018-03-17 stsp }
601 7e656b93 2018-03-17 stsp
602 c7fe698a 2018-01-23 stsp static const struct got_error *
603 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
604 c3703302 2018-01-23 stsp FILE *packfile)
605 a487c1d0 2018-01-14 stsp {
606 a487c1d0 2018-01-14 stsp uint8_t t = 0;
607 a487c1d0 2018-01-14 stsp uint64_t s = 0;
608 a487c1d0 2018-01-14 stsp uint8_t sizeN;
609 a487c1d0 2018-01-14 stsp size_t n;
610 a487c1d0 2018-01-14 stsp int i = 0;
611 a487c1d0 2018-01-14 stsp
612 a1fd68d8 2018-01-12 stsp do {
613 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
614 a487c1d0 2018-01-14 stsp if (i > 9)
615 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
616 a1fd68d8 2018-01-12 stsp
617 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
618 a487c1d0 2018-01-14 stsp if (n != 1)
619 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
620 8251fdbc 2018-01-12 stsp
621 a1fd68d8 2018-01-12 stsp if (i == 0) {
622 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
623 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
624 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
625 a1fd68d8 2018-01-12 stsp } else {
626 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
627 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
628 a1fd68d8 2018-01-12 stsp }
629 a1fd68d8 2018-01-12 stsp i++;
630 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
631 a1fd68d8 2018-01-12 stsp
632 a487c1d0 2018-01-14 stsp *type = t;
633 a487c1d0 2018-01-14 stsp *size = s;
634 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
635 a487c1d0 2018-01-14 stsp return NULL;
636 0a0a3048 2018-01-10 stsp }
637 c54542a0 2018-01-13 stsp
638 a1fd68d8 2018-01-12 stsp static const struct got_error *
639 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
640 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
641 6ccb713b 2018-01-19 stsp {
642 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
643 6ccb713b 2018-01-19 stsp if (*obj == NULL)
644 0a585a0d 2018-03-17 stsp return got_error_from_errno();
645 6ccb713b 2018-01-19 stsp
646 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
647 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
648 0a585a0d 2018-03-17 stsp const struct got_error *err = got_error_from_errno();
649 6ccb713b 2018-01-19 stsp free(*obj);
650 6ccb713b 2018-01-19 stsp *obj = NULL;
651 0a585a0d 2018-03-17 stsp return err;
652 6ccb713b 2018-01-19 stsp }
653 6ccb713b 2018-01-19 stsp
654 6ccb713b 2018-01-19 stsp (*obj)->type = type;
655 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
656 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
657 6ccb713b 2018-01-19 stsp (*obj)->size = size;
658 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
659 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
660 b107e67f 2018-01-19 stsp
661 b107e67f 2018-01-19 stsp return NULL;
662 b107e67f 2018-01-19 stsp }
663 b107e67f 2018-01-19 stsp
664 b107e67f 2018-01-19 stsp static const struct got_error *
665 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
666 b107e67f 2018-01-19 stsp {
667 b107e67f 2018-01-19 stsp int64_t o = 0;
668 b107e67f 2018-01-19 stsp uint8_t offN;
669 b107e67f 2018-01-19 stsp size_t n;
670 b107e67f 2018-01-19 stsp int i = 0;
671 b107e67f 2018-01-19 stsp
672 b107e67f 2018-01-19 stsp do {
673 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
674 b107e67f 2018-01-19 stsp if (i > 8)
675 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
676 b107e67f 2018-01-19 stsp
677 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
678 b107e67f 2018-01-19 stsp if (n != 1)
679 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
680 b107e67f 2018-01-19 stsp
681 b107e67f 2018-01-19 stsp if (i == 0)
682 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
683 b107e67f 2018-01-19 stsp else {
684 cecc778e 2018-01-23 stsp o++;
685 b107e67f 2018-01-19 stsp o <<= 7;
686 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
687 b107e67f 2018-01-19 stsp }
688 b107e67f 2018-01-19 stsp i++;
689 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
690 6ccb713b 2018-01-19 stsp
691 b107e67f 2018-01-19 stsp *offset = o;
692 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
693 6ccb713b 2018-01-19 stsp return NULL;
694 6ccb713b 2018-01-19 stsp }
695 6ccb713b 2018-01-19 stsp
696 6ccb713b 2018-01-19 stsp static const struct got_error *
697 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
698 b107e67f 2018-01-19 stsp {
699 96f5e8b3 2018-01-23 stsp const struct got_error *err;
700 b107e67f 2018-01-19 stsp int64_t negoffset;
701 b107e67f 2018-01-19 stsp size_t negofflen;
702 b107e67f 2018-01-19 stsp
703 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
704 b107e67f 2018-01-19 stsp if (err)
705 b107e67f 2018-01-19 stsp return err;
706 b107e67f 2018-01-19 stsp
707 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
708 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
709 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
710 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
711 b107e67f 2018-01-19 stsp
712 96f5e8b3 2018-01-23 stsp return NULL;
713 96f5e8b3 2018-01-23 stsp }
714 96f5e8b3 2018-01-23 stsp
715 0e22967e 2018-02-11 stsp static const struct got_error *
716 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
717 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
718 a3500804 2018-01-23 stsp
719 96f5e8b3 2018-01-23 stsp static const struct got_error *
720 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
721 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
722 a53d2f13 2018-03-17 stsp size_t delta_data_offset, uint8_t *delta_buf, size_t delta_len)
723 bdd2fbb3 2018-02-11 stsp {
724 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
725 bdd2fbb3 2018-02-11 stsp
726 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
727 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf,
728 a53d2f13 2018-03-17 stsp delta_len);
729 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
730 0a585a0d 2018-03-17 stsp return got_error_from_errno();
731 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
732 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
733 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
734 bdd2fbb3 2018-02-11 stsp return NULL;
735 bdd2fbb3 2018-02-11 stsp }
736 bdd2fbb3 2018-02-11 stsp
737 bdd2fbb3 2018-02-11 stsp static const struct got_error *
738 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
739 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
740 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
741 bdd2fbb3 2018-02-11 stsp
742 a3500804 2018-01-23 stsp {
743 a3500804 2018-01-23 stsp const struct got_error *err;
744 c3703302 2018-01-23 stsp off_t base_offset;
745 c3703302 2018-01-23 stsp uint8_t base_type;
746 c3703302 2018-01-23 stsp uint64_t base_size;
747 c3703302 2018-01-23 stsp size_t base_tslen;
748 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
749 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
750 a53d2f13 2018-03-17 stsp size_t delta_len;
751 a3500804 2018-01-23 stsp
752 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
753 a3500804 2018-01-23 stsp if (err)
754 a3500804 2018-01-23 stsp return err;
755 a3500804 2018-01-23 stsp
756 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
757 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
758 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
759 a53d2f13 2018-03-17 stsp
760 a53d2f13 2018-03-17 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, packfile);
761 a53d2f13 2018-03-17 stsp if (err)
762 a53d2f13 2018-03-17 stsp return err;
763 bdd2fbb3 2018-02-11 stsp
764 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
765 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
766 bdd2fbb3 2018-02-11 stsp if (err)
767 bdd2fbb3 2018-02-11 stsp return err;
768 bdd2fbb3 2018-02-11 stsp
769 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
770 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
771 b107e67f 2018-01-19 stsp return got_error_from_errno();
772 b107e67f 2018-01-19 stsp
773 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
774 b107e67f 2018-01-19 stsp packfile);
775 b107e67f 2018-01-19 stsp if (err)
776 b107e67f 2018-01-19 stsp return err;
777 b107e67f 2018-01-19 stsp
778 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
779 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
780 c3703302 2018-01-23 stsp }
781 c3703302 2018-01-23 stsp
782 c3703302 2018-01-23 stsp static const struct got_error *
783 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
784 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
785 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
786 c3703302 2018-01-23 stsp {
787 6b9c9673 2018-01-23 stsp const struct got_error *err;
788 6b9c9673 2018-01-23 stsp struct got_object_id id;
789 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
790 6b9c9673 2018-01-23 stsp int idx;
791 6b9c9673 2018-01-23 stsp off_t base_offset;
792 6b9c9673 2018-01-23 stsp uint8_t base_type;
793 6b9c9673 2018-01-23 stsp uint64_t base_size;
794 6b9c9673 2018-01-23 stsp size_t base_tslen;
795 6b9c9673 2018-01-23 stsp size_t n;
796 65cf1e80 2018-03-16 stsp char *path_base_packfile;
797 999f19f6 2018-03-17 stsp struct got_pack *base_pack;
798 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
799 a53d2f13 2018-03-17 stsp uint8_t *delta_buf;
800 a53d2f13 2018-03-17 stsp size_t delta_len;
801 6b9c9673 2018-01-23 stsp
802 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
803 6b9c9673 2018-01-23 stsp if (n != 1)
804 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
805 6b9c9673 2018-01-23 stsp
806 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
807 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
808 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
809 bdd2fbb3 2018-02-11 stsp
810 a53d2f13 2018-03-17 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, packfile);
811 a53d2f13 2018-03-17 stsp if (err)
812 a53d2f13 2018-03-17 stsp return err;
813 a53d2f13 2018-03-17 stsp
814 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
815 a53d2f13 2018-03-17 stsp delta_type, delta_size, delta_data_offset, delta_buf, delta_len);
816 bdd2fbb3 2018-02-11 stsp if (err)
817 bdd2fbb3 2018-02-11 stsp return err;
818 bdd2fbb3 2018-02-11 stsp
819 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
820 6b9c9673 2018-01-23 stsp if (err)
821 6b9c9673 2018-01-23 stsp return err;
822 6b9c9673 2018-01-23 stsp
823 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
824 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
825 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
826 6b9c9673 2018-01-23 stsp }
827 6b9c9673 2018-01-23 stsp
828 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_base_packfile, repo, packidx);
829 7e656b93 2018-03-17 stsp if (err)
830 7e656b93 2018-03-17 stsp return err;
831 7e656b93 2018-03-17 stsp
832 999f19f6 2018-03-17 stsp base_pack = get_cached_pack(path_base_packfile, repo);
833 999f19f6 2018-03-17 stsp if (base_pack == NULL) {
834 999f19f6 2018-03-17 stsp err = cache_pack(&base_pack, path_base_packfile, NULL, repo);
835 999f19f6 2018-03-17 stsp if (err)
836 999f19f6 2018-03-17 stsp goto done;
837 999f19f6 2018-03-17 stsp }
838 6b9c9673 2018-01-23 stsp
839 999f19f6 2018-03-17 stsp if (fseeko(base_pack->packfile, base_offset, SEEK_SET) != 0) {
840 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
841 6b9c9673 2018-01-23 stsp goto done;
842 6b9c9673 2018-01-23 stsp }
843 6b9c9673 2018-01-23 stsp
844 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
845 999f19f6 2018-03-17 stsp base_pack->packfile);
846 6b9c9673 2018-01-23 stsp if (err)
847 6b9c9673 2018-01-23 stsp goto done;
848 6b9c9673 2018-01-23 stsp
849 999f19f6 2018-03-17 stsp err = resolve_delta_chain(deltas, repo, base_pack->packfile,
850 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
851 0e22967e 2018-02-11 stsp base_size);
852 6b9c9673 2018-01-23 stsp done:
853 65cf1e80 2018-03-16 stsp free(path_base_packfile);
854 6b9c9673 2018-01-23 stsp return err;
855 6b9c9673 2018-01-23 stsp }
856 6b9c9673 2018-01-23 stsp
857 6b9c9673 2018-01-23 stsp static const struct got_error *
858 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
859 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
860 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
861 6b9c9673 2018-01-23 stsp {
862 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
863 c3703302 2018-01-23 stsp
864 c3703302 2018-01-23 stsp switch (delta_type) {
865 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
866 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
867 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
868 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
869 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
870 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
871 a53d2f13 2018-03-17 stsp delta_type, delta_size, 0, NULL, 0);
872 4e8cda55 2018-01-19 stsp break;
873 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
874 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
875 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
876 bdd2fbb3 2018-02-11 stsp delta_size);
877 96f5e8b3 2018-01-23 stsp break;
878 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
879 bdd2fbb3 2018-02-11 stsp err = resolve_ref_delta(deltas, repo, packfile,
880 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
881 bdd2fbb3 2018-02-11 stsp delta_size);
882 6b9c9673 2018-01-23 stsp break;
883 4e8cda55 2018-01-19 stsp default:
884 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
885 4e8cda55 2018-01-19 stsp }
886 96f5e8b3 2018-01-23 stsp
887 96f5e8b3 2018-01-23 stsp return err;
888 96f5e8b3 2018-01-23 stsp }
889 96f5e8b3 2018-01-23 stsp
890 96f5e8b3 2018-01-23 stsp static const struct got_error *
891 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
892 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
893 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
894 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
895 96f5e8b3 2018-01-23 stsp {
896 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
897 96f5e8b3 2018-01-23 stsp int resolved_type;
898 4e8cda55 2018-01-19 stsp
899 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
900 b107e67f 2018-01-19 stsp if (*obj == NULL)
901 0a585a0d 2018-03-17 stsp return got_error_from_errno();
902 b107e67f 2018-01-19 stsp
903 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
904 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
905 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
906 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
907 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
908 b107e67f 2018-01-19 stsp
909 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
910 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
911 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
912 96f5e8b3 2018-01-23 stsp goto done;
913 96f5e8b3 2018-01-23 stsp }
914 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
915 96f5e8b3 2018-01-23 stsp
916 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
917 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
918 c3703302 2018-01-23 stsp
919 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
920 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
921 96f5e8b3 2018-01-23 stsp if (err)
922 96f5e8b3 2018-01-23 stsp goto done;
923 96f5e8b3 2018-01-23 stsp
924 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
925 96f5e8b3 2018-01-23 stsp if (err)
926 96f5e8b3 2018-01-23 stsp goto done;
927 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
928 96f5e8b3 2018-01-23 stsp
929 96f5e8b3 2018-01-23 stsp done:
930 96f5e8b3 2018-01-23 stsp if (err) {
931 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
932 96f5e8b3 2018-01-23 stsp *obj = NULL;
933 96f5e8b3 2018-01-23 stsp }
934 96f5e8b3 2018-01-23 stsp return err;
935 b107e67f 2018-01-19 stsp }
936 b107e67f 2018-01-19 stsp
937 b107e67f 2018-01-19 stsp static const struct got_error *
938 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
939 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
940 a1fd68d8 2018-01-12 stsp {
941 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
942 a1fd68d8 2018-01-12 stsp off_t offset;
943 65cf1e80 2018-03-16 stsp char *path_packfile;
944 6d89869a 2018-03-17 stsp struct got_pack *pack;
945 6c00b545 2018-01-17 stsp uint8_t type;
946 6c00b545 2018-01-17 stsp uint64_t size;
947 3ee5fc21 2018-01-17 stsp size_t tslen;
948 a1fd68d8 2018-01-12 stsp
949 6c00b545 2018-01-17 stsp *obj = NULL;
950 a1fd68d8 2018-01-12 stsp
951 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
952 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
953 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
954 a1fd68d8 2018-01-12 stsp
955 7e656b93 2018-03-17 stsp err = get_packfile_path(&path_packfile, repo, packidx);
956 6b9c9673 2018-01-23 stsp if (err)
957 6b9c9673 2018-01-23 stsp return err;
958 a1fd68d8 2018-01-12 stsp
959 6d89869a 2018-03-17 stsp pack = get_cached_pack(path_packfile, repo);
960 6d89869a 2018-03-17 stsp if (pack == NULL) {
961 6d89869a 2018-03-17 stsp err = cache_pack(&pack, path_packfile, packidx, repo);
962 6d89869a 2018-03-17 stsp if (err)
963 6d89869a 2018-03-17 stsp goto done;
964 6d89869a 2018-03-17 stsp }
965 7e656b93 2018-03-17 stsp
966 6d89869a 2018-03-17 stsp if (fseeko(pack->packfile, offset, SEEK_SET) != 0) {
967 6c00b545 2018-01-17 stsp err = got_error_from_errno();
968 6c00b545 2018-01-17 stsp goto done;
969 6c00b545 2018-01-17 stsp }
970 6c00b545 2018-01-17 stsp
971 6d89869a 2018-03-17 stsp err = parse_object_type_and_size(&type, &size, &tslen, pack->packfile);
972 a1fd68d8 2018-01-12 stsp if (err)
973 a1fd68d8 2018-01-12 stsp goto done;
974 a1fd68d8 2018-01-12 stsp
975 6c00b545 2018-01-17 stsp switch (type) {
976 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
977 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
978 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
979 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
980 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
981 6ccb713b 2018-01-19 stsp offset + tslen, size);
982 6c00b545 2018-01-17 stsp break;
983 6ccb713b 2018-01-19 stsp
984 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
985 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
986 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
987 6d89869a 2018-03-17 stsp pack->packfile, id, offset, tslen, type, size);
988 b107e67f 2018-01-19 stsp break;
989 b107e67f 2018-01-19 stsp
990 6c00b545 2018-01-17 stsp default:
991 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
992 6c00b545 2018-01-17 stsp goto done;
993 6c00b545 2018-01-17 stsp }
994 a1fd68d8 2018-01-12 stsp done:
995 65cf1e80 2018-03-16 stsp free(path_packfile);
996 a1fd68d8 2018-01-12 stsp return err;
997 a1fd68d8 2018-01-12 stsp }
998 a1fd68d8 2018-01-12 stsp
999 a1fd68d8 2018-01-12 stsp const struct got_error *
1000 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
1001 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
1002 a1fd68d8 2018-01-12 stsp {
1003 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
1004 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
1005 6b9c9673 2018-01-23 stsp int idx;
1006 a1fd68d8 2018-01-12 stsp
1007 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
1008 6b9c9673 2018-01-23 stsp if (err)
1009 6b9c9673 2018-01-23 stsp return err;
1010 a1fd68d8 2018-01-12 stsp
1011 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
1012 7e656b93 2018-03-17 stsp if (err)
1013 7e656b93 2018-03-17 stsp return err;
1014 7e656b93 2018-03-17 stsp
1015 7e656b93 2018-03-17 stsp err = cache_pack(NULL, (*obj)->path_packfile, packidx, repo);
1016 3ee5fc21 2018-01-17 stsp return err;
1017 3ee5fc21 2018-01-17 stsp }
1018 efd2a263 2018-01-19 stsp
1019 efd2a263 2018-01-19 stsp static const struct got_error *
1020 40426839 2018-03-17 stsp get_delta_chain_max_size(uint64_t *max_size, struct got_delta_chain *deltas)
1021 22484865 2018-03-13 stsp {
1022 22484865 2018-03-13 stsp struct got_delta *delta;
1023 22484865 2018-03-13 stsp uint64_t base_size = 0, result_size = 0;
1024 22484865 2018-03-13 stsp
1025 22484865 2018-03-13 stsp *max_size = 0;
1026 22484865 2018-03-13 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1027 22484865 2018-03-13 stsp /* Plain object types are the delta base. */
1028 22484865 2018-03-13 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1029 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1030 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1031 22484865 2018-03-13 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1032 22484865 2018-03-13 stsp const struct got_error *err;
1033 a53d2f13 2018-03-17 stsp err = got_delta_get_sizes(&base_size, &result_size,
1034 a53d2f13 2018-03-17 stsp delta->delta_buf, delta->delta_len);
1035 22484865 2018-03-13 stsp if (err)
1036 22484865 2018-03-13 stsp return err;
1037 22484865 2018-03-13 stsp } else
1038 22484865 2018-03-13 stsp base_size = delta->size;
1039 22484865 2018-03-13 stsp if (base_size > *max_size)
1040 22484865 2018-03-13 stsp *max_size = base_size;
1041 22484865 2018-03-13 stsp if (result_size > *max_size)
1042 22484865 2018-03-13 stsp *max_size = result_size;
1043 22484865 2018-03-13 stsp }
1044 bd1223b9 2018-03-14 stsp
1045 9feb4ff2 2018-03-14 stsp return NULL;
1046 22484865 2018-03-13 stsp }
1047 22484865 2018-03-13 stsp
1048 22484865 2018-03-13 stsp static const struct got_error *
1049 b29656e2 2018-03-16 stsp dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
1050 f7e127f3 2018-03-17 stsp FILE *outfile, struct got_pack *pack, struct got_repository *repo)
1051 efd2a263 2018-01-19 stsp {
1052 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
1053 6691714a 2018-01-23 stsp struct got_delta *delta;
1054 22484865 2018-03-13 stsp FILE *base_file = NULL, *accum_file = NULL;
1055 8628c62d 2018-03-15 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1056 b29656e2 2018-03-16 stsp size_t accum_size = 0;
1057 22484865 2018-03-13 stsp uint64_t max_size;
1058 6691714a 2018-01-23 stsp int n = 0;
1059 b29656e2 2018-03-16 stsp
1060 b29656e2 2018-03-16 stsp *result_size = 0;
1061 3ee5fc21 2018-01-17 stsp
1062 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1063 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1064 efd2a263 2018-01-19 stsp
1065 8628c62d 2018-03-15 stsp /* We process small enough files entirely in memory for speed. */
1066 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1067 22484865 2018-03-13 stsp if (err)
1068 22484865 2018-03-13 stsp return err;
1069 8628c62d 2018-03-15 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1070 8628c62d 2018-03-15 stsp accum_buf = malloc(max_size);
1071 8628c62d 2018-03-15 stsp if (accum_buf == NULL)
1072 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1073 8628c62d 2018-03-15 stsp } else {
1074 22484865 2018-03-13 stsp base_file = got_opentemp();
1075 8628c62d 2018-03-15 stsp if (base_file == NULL)
1076 8628c62d 2018-03-15 stsp return got_error_from_errno();
1077 efd2a263 2018-01-19 stsp
1078 22484865 2018-03-13 stsp accum_file = got_opentemp();
1079 8628c62d 2018-03-15 stsp if (accum_file == NULL) {
1080 8628c62d 2018-03-15 stsp err = got_error_from_errno();
1081 8628c62d 2018-03-15 stsp fclose(base_file);
1082 8628c62d 2018-03-15 stsp return err;
1083 8628c62d 2018-03-15 stsp }
1084 6691714a 2018-01-23 stsp }
1085 efd2a263 2018-01-19 stsp
1086 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
1087 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1088 a6b158cc 2018-02-11 stsp if (n == 0) {
1089 8628c62d 2018-03-15 stsp size_t base_len;
1090 9db65d41 2018-03-14 stsp
1091 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
1092 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1093 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1094 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1095 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1096 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1097 a6b158cc 2018-02-11 stsp goto done;
1098 a6b158cc 2018-02-11 stsp }
1099 6691714a 2018-01-23 stsp
1100 f7e127f3 2018-03-17 stsp if (fseeko(pack->packfile, delta->offset + delta->tslen,
1101 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
1102 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
1103 bdd2fbb3 2018-02-11 stsp goto done;
1104 bdd2fbb3 2018-02-11 stsp }
1105 8628c62d 2018-03-15 stsp if (base_file)
1106 7e656b93 2018-03-17 stsp err = got_inflate_to_file(&base_len,
1107 f7e127f3 2018-03-17 stsp pack->packfile, base_file);
1108 8628c62d 2018-03-15 stsp else {
1109 8628c62d 2018-03-15 stsp err = got_inflate_to_mem(&base_buf, &base_len,
1110 f7e127f3 2018-03-17 stsp pack->packfile);
1111 8628c62d 2018-03-15 stsp if (base_len < max_size) {
1112 8628c62d 2018-03-15 stsp uint8_t *p;
1113 8628c62d 2018-03-15 stsp p = reallocarray(base_buf, 1, max_size);
1114 8628c62d 2018-03-15 stsp if (p == NULL) {
1115 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1116 8628c62d 2018-03-15 stsp goto done;
1117 8628c62d 2018-03-15 stsp }
1118 8628c62d 2018-03-15 stsp base_buf = p;
1119 8628c62d 2018-03-15 stsp }
1120 8628c62d 2018-03-15 stsp }
1121 a6b158cc 2018-02-11 stsp if (err)
1122 a6b158cc 2018-02-11 stsp goto done;
1123 a6b158cc 2018-02-11 stsp n++;
1124 8628c62d 2018-03-15 stsp if (base_file)
1125 8628c62d 2018-03-15 stsp rewind(base_file);
1126 a6b158cc 2018-02-11 stsp continue;
1127 9db65d41 2018-03-14 stsp }
1128 885d3e02 2018-01-27 stsp
1129 8628c62d 2018-03-15 stsp if (base_buf) {
1130 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1131 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1132 8628c62d 2018-03-15 stsp n++;
1133 8628c62d 2018-03-15 stsp } else {
1134 a53d2f13 2018-03-17 stsp err = got_delta_apply(base_file, delta->delta_buf,
1135 a53d2f13 2018-03-17 stsp delta->delta_len,
1136 8628c62d 2018-03-15 stsp /* Final delta application writes to output file. */
1137 b29656e2 2018-03-16 stsp ++n < deltas->nentries ? accum_file : outfile,
1138 b29656e2 2018-03-16 stsp &accum_size);
1139 8628c62d 2018-03-15 stsp }
1140 6691714a 2018-01-23 stsp if (err)
1141 6691714a 2018-01-23 stsp goto done;
1142 6691714a 2018-01-23 stsp
1143 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1144 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1145 8628c62d 2018-03-15 stsp if (base_buf) {
1146 8628c62d 2018-03-15 stsp uint8_t *tmp = accum_buf;
1147 8628c62d 2018-03-15 stsp accum_buf = base_buf;
1148 8628c62d 2018-03-15 stsp base_buf = tmp;
1149 8628c62d 2018-03-15 stsp } else {
1150 8628c62d 2018-03-15 stsp FILE *tmp = accum_file;
1151 8628c62d 2018-03-15 stsp accum_file = base_file;
1152 8628c62d 2018-03-15 stsp base_file = tmp;
1153 8628c62d 2018-03-15 stsp rewind(base_file);
1154 8628c62d 2018-03-15 stsp rewind(accum_file);
1155 8628c62d 2018-03-15 stsp }
1156 6691714a 2018-01-23 stsp }
1157 6691714a 2018-01-23 stsp }
1158 6691714a 2018-01-23 stsp
1159 6691714a 2018-01-23 stsp done:
1160 8628c62d 2018-03-15 stsp free(base_buf);
1161 8628c62d 2018-03-15 stsp if (accum_buf) {
1162 8628c62d 2018-03-15 stsp size_t len = fwrite(accum_buf, 1, accum_size, outfile);
1163 8628c62d 2018-03-15 stsp free(accum_buf);
1164 8628c62d 2018-03-15 stsp if (len != accum_size)
1165 8628c62d 2018-03-15 stsp return got_ferror(outfile, GOT_ERR_IO);
1166 8628c62d 2018-03-15 stsp }
1167 8628c62d 2018-03-15 stsp if (base_file)
1168 8628c62d 2018-03-15 stsp fclose(base_file);
1169 8628c62d 2018-03-15 stsp if (accum_file)
1170 8628c62d 2018-03-15 stsp fclose(accum_file);
1171 6691714a 2018-01-23 stsp rewind(outfile);
1172 b29656e2 2018-03-16 stsp if (err == NULL)
1173 b29656e2 2018-03-16 stsp *result_size = accum_size;
1174 e0ab43e7 2018-03-16 stsp return err;
1175 e0ab43e7 2018-03-16 stsp }
1176 e0ab43e7 2018-03-16 stsp
1177 e0ab43e7 2018-03-16 stsp static const struct got_error *
1178 e0ab43e7 2018-03-16 stsp dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
1179 f7e127f3 2018-03-17 stsp struct got_delta_chain *deltas, struct got_pack *pack,
1180 e0ab43e7 2018-03-16 stsp struct got_repository *repo)
1181 e0ab43e7 2018-03-16 stsp {
1182 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1183 e0ab43e7 2018-03-16 stsp struct got_delta *delta;
1184 e0ab43e7 2018-03-16 stsp uint8_t *base_buf = NULL, *accum_buf = NULL;
1185 e0ab43e7 2018-03-16 stsp size_t accum_size;
1186 e0ab43e7 2018-03-16 stsp uint64_t max_size;
1187 e0ab43e7 2018-03-16 stsp int n = 0;
1188 e0ab43e7 2018-03-16 stsp
1189 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1190 e0ab43e7 2018-03-16 stsp *outlen = 0;
1191 e0ab43e7 2018-03-16 stsp
1192 e0ab43e7 2018-03-16 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
1193 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
1194 e0ab43e7 2018-03-16 stsp
1195 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, deltas);
1196 e0ab43e7 2018-03-16 stsp if (err)
1197 e0ab43e7 2018-03-16 stsp return err;
1198 e0ab43e7 2018-03-16 stsp accum_buf = malloc(max_size);
1199 e0ab43e7 2018-03-16 stsp if (accum_buf == NULL)
1200 0a585a0d 2018-03-17 stsp return got_error_from_errno();
1201 e0ab43e7 2018-03-16 stsp
1202 e0ab43e7 2018-03-16 stsp /* Deltas are ordered in ascending order. */
1203 e0ab43e7 2018-03-16 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
1204 e0ab43e7 2018-03-16 stsp if (n == 0) {
1205 e0ab43e7 2018-03-16 stsp size_t base_len;
1206 e0ab43e7 2018-03-16 stsp
1207 e0ab43e7 2018-03-16 stsp /* Plain object types are the delta base. */
1208 e0ab43e7 2018-03-16 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
1209 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TREE &&
1210 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
1211 e0ab43e7 2018-03-16 stsp delta->type != GOT_OBJ_TYPE_TAG) {
1212 e0ab43e7 2018-03-16 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
1213 e0ab43e7 2018-03-16 stsp goto done;
1214 e0ab43e7 2018-03-16 stsp }
1215 e0ab43e7 2018-03-16 stsp
1216 f7e127f3 2018-03-17 stsp if (fseeko(pack->packfile, delta->offset + delta->tslen,
1217 e0ab43e7 2018-03-16 stsp SEEK_SET) != 0) {
1218 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1219 e0ab43e7 2018-03-16 stsp goto done;
1220 e0ab43e7 2018-03-16 stsp }
1221 e0ab43e7 2018-03-16 stsp err = got_inflate_to_mem(&base_buf, &base_len,
1222 f7e127f3 2018-03-17 stsp pack->packfile);
1223 e0ab43e7 2018-03-16 stsp if (base_len < max_size) {
1224 e0ab43e7 2018-03-16 stsp uint8_t *p;
1225 e0ab43e7 2018-03-16 stsp p = reallocarray(base_buf, 1, max_size);
1226 e0ab43e7 2018-03-16 stsp if (p == NULL) {
1227 0a585a0d 2018-03-17 stsp err = got_error_from_errno();
1228 e0ab43e7 2018-03-16 stsp goto done;
1229 e0ab43e7 2018-03-16 stsp }
1230 e0ab43e7 2018-03-16 stsp base_buf = p;
1231 e0ab43e7 2018-03-16 stsp }
1232 e0ab43e7 2018-03-16 stsp if (err)
1233 e0ab43e7 2018-03-16 stsp goto done;
1234 e0ab43e7 2018-03-16 stsp n++;
1235 e0ab43e7 2018-03-16 stsp continue;
1236 e0ab43e7 2018-03-16 stsp }
1237 e0ab43e7 2018-03-16 stsp
1238 a53d2f13 2018-03-17 stsp err = got_delta_apply_in_mem(base_buf, delta->delta_buf,
1239 a53d2f13 2018-03-17 stsp delta->delta_len, accum_buf, &accum_size);
1240 e0ab43e7 2018-03-16 stsp n++;
1241 e0ab43e7 2018-03-16 stsp if (err)
1242 e0ab43e7 2018-03-16 stsp goto done;
1243 e0ab43e7 2018-03-16 stsp
1244 e0ab43e7 2018-03-16 stsp if (n < deltas->nentries) {
1245 e0ab43e7 2018-03-16 stsp /* Accumulated delta becomes the new base. */
1246 e0ab43e7 2018-03-16 stsp uint8_t *tmp = accum_buf;
1247 e0ab43e7 2018-03-16 stsp accum_buf = base_buf;
1248 e0ab43e7 2018-03-16 stsp base_buf = tmp;
1249 e0ab43e7 2018-03-16 stsp }
1250 e0ab43e7 2018-03-16 stsp }
1251 e0ab43e7 2018-03-16 stsp
1252 e0ab43e7 2018-03-16 stsp done:
1253 e0ab43e7 2018-03-16 stsp free(base_buf);
1254 e0ab43e7 2018-03-16 stsp if (err) {
1255 e0ab43e7 2018-03-16 stsp free(accum_buf);
1256 e0ab43e7 2018-03-16 stsp *outbuf = NULL;
1257 e0ab43e7 2018-03-16 stsp *outlen = 0;
1258 e0ab43e7 2018-03-16 stsp } else {
1259 e0ab43e7 2018-03-16 stsp *outbuf = accum_buf;
1260 e0ab43e7 2018-03-16 stsp *outlen = accum_size;
1261 e0ab43e7 2018-03-16 stsp }
1262 efd2a263 2018-01-19 stsp return err;
1263 efd2a263 2018-01-19 stsp }
1264 efd2a263 2018-01-19 stsp
1265 3ee5fc21 2018-01-17 stsp const struct got_error *
1266 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1267 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1268 3ee5fc21 2018-01-17 stsp {
1269 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1270 7e656b93 2018-03-17 stsp struct got_pack *pack;
1271 3ee5fc21 2018-01-17 stsp
1272 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1273 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1274 3ee5fc21 2018-01-17 stsp
1275 7e656b93 2018-03-17 stsp pack = get_cached_pack(obj->path_packfile, repo);
1276 7e656b93 2018-03-17 stsp if (pack == NULL) {
1277 7e656b93 2018-03-17 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1278 7e656b93 2018-03-17 stsp if (err)
1279 7e656b93 2018-03-17 stsp goto done;
1280 ef2bccd9 2018-03-16 stsp }
1281 3ee5fc21 2018-01-17 stsp
1282 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1283 7e656b93 2018-03-17 stsp if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
1284 6691714a 2018-01-23 stsp err = got_error_from_errno();
1285 6691714a 2018-01-23 stsp goto done;
1286 6691714a 2018-01-23 stsp }
1287 3ee5fc21 2018-01-17 stsp
1288 0bd0053c 2018-03-17 stsp if (obj->size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
1289 0bd0053c 2018-03-17 stsp size_t size = obj->size;
1290 0bd0053c 2018-03-17 stsp if (size == 0) /* empty file */
1291 0bd0053c 2018-03-17 stsp size = 1;
1292 0bd0053c 2018-03-17 stsp *f = fmemopen(NULL, size, "w+");
1293 0bd0053c 2018-03-17 stsp } else
1294 c52ac529 2018-03-17 stsp *f = got_opentemp();
1295 c52ac529 2018-03-17 stsp if (*f == NULL) {
1296 c52ac529 2018-03-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
1297 c52ac529 2018-03-17 stsp goto done;
1298 c52ac529 2018-03-17 stsp }
1299 7e656b93 2018-03-17 stsp err = got_inflate_to_file(&obj->size, pack->packfile, *f);
1300 c52ac529 2018-03-17 stsp } else {
1301 c52ac529 2018-03-17 stsp uint64_t max_size;
1302 c52ac529 2018-03-17 stsp
1303 40426839 2018-03-17 stsp err = get_delta_chain_max_size(&max_size, &obj->deltas);
1304 c52ac529 2018-03-17 stsp if (err)
1305 c52ac529 2018-03-17 stsp return err;
1306 c52ac529 2018-03-17 stsp if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX)
1307 c52ac529 2018-03-17 stsp *f = fmemopen(NULL, max_size, "w+");
1308 c52ac529 2018-03-17 stsp else
1309 c52ac529 2018-03-17 stsp *f = got_opentemp();
1310 c52ac529 2018-03-17 stsp if (*f == NULL) {
1311 c52ac529 2018-03-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
1312 c52ac529 2018-03-17 stsp goto done;
1313 c52ac529 2018-03-17 stsp }
1314 b29656e2 2018-03-16 stsp err = dump_delta_chain_to_file(&obj->size, &obj->deltas, *f,
1315 f7e127f3 2018-03-17 stsp pack, repo);
1316 c52ac529 2018-03-17 stsp }
1317 3ee5fc21 2018-01-17 stsp done:
1318 d0f3be7c 2018-03-17 stsp if (err && *f) {
1319 3ee5fc21 2018-01-17 stsp fclose(*f);
1320 d0f3be7c 2018-03-17 stsp *f = NULL;
1321 d0f3be7c 2018-03-17 stsp }
1322 a1fd68d8 2018-01-12 stsp return err;
1323 a1fd68d8 2018-01-12 stsp }
1324 e0ab43e7 2018-03-16 stsp
1325 e0ab43e7 2018-03-16 stsp const struct got_error *
1326 e0ab43e7 2018-03-16 stsp got_packfile_extract_object_to_mem(uint8_t **buf, size_t *len,
1327 e0ab43e7 2018-03-16 stsp struct got_object *obj, struct got_repository *repo)
1328 e0ab43e7 2018-03-16 stsp {
1329 e0ab43e7 2018-03-16 stsp const struct got_error *err = NULL;
1330 e0ab43e7 2018-03-16 stsp FILE *packfile = NULL;
1331 7e656b93 2018-03-17 stsp struct got_pack *pack;
1332 e0ab43e7 2018-03-16 stsp
1333 e0ab43e7 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1334 e0ab43e7 2018-03-16 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1335 e0ab43e7 2018-03-16 stsp
1336 7e656b93 2018-03-17 stsp pack = get_cached_pack(obj->path_packfile, repo);
1337 7e656b93 2018-03-17 stsp if (pack == NULL) {
1338 7e656b93 2018-03-17 stsp err = cache_pack(&pack, obj->path_packfile, NULL, repo);
1339 7e656b93 2018-03-17 stsp if (err)
1340 7e656b93 2018-03-17 stsp goto done;
1341 ef2bccd9 2018-03-16 stsp }
1342 e0ab43e7 2018-03-16 stsp
1343 ef2bccd9 2018-03-16 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1344 7e656b93 2018-03-17 stsp if (fseeko(pack->packfile, obj->pack_offset, SEEK_SET) != 0) {
1345 e0ab43e7 2018-03-16 stsp err = got_error_from_errno();
1346 e0ab43e7 2018-03-16 stsp goto done;
1347 e0ab43e7 2018-03-16 stsp }
1348 e0ab43e7 2018-03-16 stsp
1349 7e656b93 2018-03-17 stsp err = got_inflate_to_mem(buf, len, pack->packfile);
1350 e0ab43e7 2018-03-16 stsp } else
1351 f7e127f3 2018-03-17 stsp err = dump_delta_chain_to_mem(buf, len, &obj->deltas, pack,
1352 f7e127f3 2018-03-17 stsp repo);
1353 e0ab43e7 2018-03-16 stsp done:
1354 e0ab43e7 2018-03-16 stsp if (packfile)
1355 e0ab43e7 2018-03-16 stsp fclose(packfile);
1356 e0ab43e7 2018-03-16 stsp return err;
1357 e0ab43e7 2018-03-16 stsp }