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