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