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