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 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
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 79b11c62 2018-03-09 stsp static void
368 63afc6c6 2018-03-10 stsp cache_packidx(struct got_packidx_v2_hdr *packidx, struct got_repository *repo)
369 79b11c62 2018-03-09 stsp {
370 79b11c62 2018-03-09 stsp int i;
371 79b11c62 2018-03-09 stsp
372 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
373 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
374 79b11c62 2018-03-09 stsp break;
375 79b11c62 2018-03-09 stsp }
376 79b11c62 2018-03-09 stsp
377 79b11c62 2018-03-09 stsp if (i == nitems(repo->packidx_cache)) {
378 79b11c62 2018-03-09 stsp got_packidx_close(repo->packidx_cache[i - 1]);
379 79b11c62 2018-03-09 stsp memmove(&repo->packidx_cache[1], &repo->packidx_cache[0],
380 79b11c62 2018-03-09 stsp sizeof(repo->packidx_cache) -
381 79b11c62 2018-03-09 stsp sizeof(repo->packidx_cache[0]));
382 79b11c62 2018-03-09 stsp i = 0;
383 79b11c62 2018-03-09 stsp }
384 79b11c62 2018-03-09 stsp
385 79b11c62 2018-03-09 stsp repo->packidx_cache[i] = dup_packidx(packidx);
386 79b11c62 2018-03-09 stsp }
387 79b11c62 2018-03-09 stsp
388 6b9c9673 2018-01-23 stsp static const struct got_error *
389 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
390 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
391 6b9c9673 2018-01-23 stsp {
392 6b9c9673 2018-01-23 stsp const struct got_error *err;
393 6b9c9673 2018-01-23 stsp char *path_packdir;
394 6b9c9673 2018-01-23 stsp DIR *packdir;
395 6b9c9673 2018-01-23 stsp struct dirent *dent;
396 6b9c9673 2018-01-23 stsp char *path_packidx;
397 79b11c62 2018-03-09 stsp int i;
398 6b9c9673 2018-01-23 stsp
399 79b11c62 2018-03-09 stsp /* Search pack index cache. */
400 79b11c62 2018-03-09 stsp for (i = 0; i < nitems(repo->packidx_cache); i++) {
401 79b11c62 2018-03-09 stsp if (repo->packidx_cache[i] == NULL)
402 79b11c62 2018-03-09 stsp break;
403 79b11c62 2018-03-09 stsp *idx = get_object_idx(repo->packidx_cache[i], id);
404 79b11c62 2018-03-09 stsp if (*idx != -1) {
405 79b11c62 2018-03-09 stsp *packidx = dup_packidx(repo->packidx_cache[i]);
406 79b11c62 2018-03-09 stsp if (*packidx == NULL)
407 0a71ee67 2018-03-09 stsp return got_error(GOT_ERR_NO_MEM);
408 79b11c62 2018-03-09 stsp return NULL;
409 79b11c62 2018-03-09 stsp }
410 79b11c62 2018-03-09 stsp }
411 79b11c62 2018-03-09 stsp /* No luck. Search the filesystem. */
412 79b11c62 2018-03-09 stsp
413 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
414 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
415 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
416 6b9c9673 2018-01-23 stsp
417 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
418 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
419 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
420 6b9c9673 2018-01-23 stsp goto done;
421 6b9c9673 2018-01-23 stsp }
422 6b9c9673 2018-01-23 stsp
423 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
424 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
425 6b9c9673 2018-01-23 stsp continue;
426 6b9c9673 2018-01-23 stsp
427 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
428 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
429 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
430 6b9c9673 2018-01-23 stsp goto done;
431 6b9c9673 2018-01-23 stsp }
432 6b9c9673 2018-01-23 stsp
433 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
434 6b9c9673 2018-01-23 stsp free(path_packidx);
435 6b9c9673 2018-01-23 stsp if (err)
436 6b9c9673 2018-01-23 stsp goto done;
437 6b9c9673 2018-01-23 stsp
438 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
439 6b9c9673 2018-01-23 stsp if (*idx != -1) {
440 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
441 79b11c62 2018-03-09 stsp cache_packidx(*packidx, repo);
442 6b9c9673 2018-01-23 stsp goto done;
443 6b9c9673 2018-01-23 stsp }
444 6b9c9673 2018-01-23 stsp
445 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
446 6b9c9673 2018-01-23 stsp *packidx = NULL;
447 6b9c9673 2018-01-23 stsp }
448 6b9c9673 2018-01-23 stsp
449 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
450 6b9c9673 2018-01-23 stsp done:
451 6b9c9673 2018-01-23 stsp free(path_packdir);
452 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
453 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
454 6b9c9673 2018-01-23 stsp return err;
455 6b9c9673 2018-01-23 stsp }
456 6b9c9673 2018-01-23 stsp
457 c7fe698a 2018-01-23 stsp static const struct got_error *
458 6b9c9673 2018-01-23 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
459 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx)
460 6b9c9673 2018-01-23 stsp {
461 6b9c9673 2018-01-23 stsp char *path_packdir;
462 6b9c9673 2018-01-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
463 6b9c9673 2018-01-23 stsp char *sha1str;
464 6b9c9673 2018-01-23 stsp
465 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
466 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
467 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
468 6b9c9673 2018-01-23 stsp
469 6b9c9673 2018-01-23 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
470 6b9c9673 2018-01-23 stsp hex, sizeof(hex));
471 6b9c9673 2018-01-23 stsp if (sha1str == NULL)
472 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
473 6b9c9673 2018-01-23 stsp
474 6b9c9673 2018-01-23 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
475 6b9c9673 2018-01-23 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
476 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
477 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
478 6b9c9673 2018-01-23 stsp }
479 6b9c9673 2018-01-23 stsp
480 6b9c9673 2018-01-23 stsp return NULL;
481 a1fd68d8 2018-01-12 stsp }
482 a1fd68d8 2018-01-12 stsp
483 a1fd68d8 2018-01-12 stsp const struct got_error *
484 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
485 a1fd68d8 2018-01-12 stsp {
486 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
487 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
488 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
489 a1fd68d8 2018-01-12 stsp size_t n;
490 a1fd68d8 2018-01-12 stsp
491 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
492 a1fd68d8 2018-01-12 stsp if (n != 1)
493 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
494 a1fd68d8 2018-01-12 stsp
495 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
496 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
497 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
498 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
499 a1fd68d8 2018-01-12 stsp
500 a1fd68d8 2018-01-12 stsp return err;
501 a487c1d0 2018-01-14 stsp }
502 a487c1d0 2018-01-14 stsp
503 a487c1d0 2018-01-14 stsp static const struct got_error *
504 c7fe698a 2018-01-23 stsp open_packfile(FILE **packfile, char **path_packfile,
505 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
506 c7fe698a 2018-01-23 stsp {
507 c7fe698a 2018-01-23 stsp const struct got_error *err;
508 c7fe698a 2018-01-23 stsp
509 c7fe698a 2018-01-23 stsp *packfile = NULL;
510 c7fe698a 2018-01-23 stsp
511 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
512 c7fe698a 2018-01-23 stsp if (err)
513 c7fe698a 2018-01-23 stsp return err;
514 c7fe698a 2018-01-23 stsp
515 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
516 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
517 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
518 c7fe698a 2018-01-23 stsp free(*path_packfile);
519 c7fe698a 2018-01-23 stsp return err;
520 c7fe698a 2018-01-23 stsp }
521 c7fe698a 2018-01-23 stsp
522 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
523 c7fe698a 2018-01-23 stsp if (err) {
524 c7fe698a 2018-01-23 stsp fclose(*packfile);
525 c7fe698a 2018-01-23 stsp *packfile = NULL;
526 c7fe698a 2018-01-23 stsp }
527 c7fe698a 2018-01-23 stsp return err;
528 c7fe698a 2018-01-23 stsp }
529 c7fe698a 2018-01-23 stsp
530 c7fe698a 2018-01-23 stsp static const struct got_error *
531 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
532 c3703302 2018-01-23 stsp FILE *packfile)
533 a487c1d0 2018-01-14 stsp {
534 a487c1d0 2018-01-14 stsp uint8_t t = 0;
535 a487c1d0 2018-01-14 stsp uint64_t s = 0;
536 a487c1d0 2018-01-14 stsp uint8_t sizeN;
537 a487c1d0 2018-01-14 stsp size_t n;
538 a487c1d0 2018-01-14 stsp int i = 0;
539 a487c1d0 2018-01-14 stsp
540 a1fd68d8 2018-01-12 stsp do {
541 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
542 a487c1d0 2018-01-14 stsp if (i > 9)
543 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
544 a1fd68d8 2018-01-12 stsp
545 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
546 a487c1d0 2018-01-14 stsp if (n != 1)
547 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
548 8251fdbc 2018-01-12 stsp
549 a1fd68d8 2018-01-12 stsp if (i == 0) {
550 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
551 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
552 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
553 a1fd68d8 2018-01-12 stsp } else {
554 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
555 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
556 a1fd68d8 2018-01-12 stsp }
557 a1fd68d8 2018-01-12 stsp i++;
558 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
559 a1fd68d8 2018-01-12 stsp
560 a487c1d0 2018-01-14 stsp *type = t;
561 a487c1d0 2018-01-14 stsp *size = s;
562 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
563 a487c1d0 2018-01-14 stsp return NULL;
564 0a0a3048 2018-01-10 stsp }
565 c54542a0 2018-01-13 stsp
566 a1fd68d8 2018-01-12 stsp static const struct got_error *
567 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
568 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
569 6ccb713b 2018-01-19 stsp {
570 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
571 6ccb713b 2018-01-19 stsp if (*obj == NULL)
572 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
573 6ccb713b 2018-01-19 stsp
574 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
575 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
576 6ccb713b 2018-01-19 stsp free(*obj);
577 6ccb713b 2018-01-19 stsp *obj = NULL;
578 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
579 6ccb713b 2018-01-19 stsp }
580 6ccb713b 2018-01-19 stsp
581 6ccb713b 2018-01-19 stsp (*obj)->type = type;
582 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
583 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
584 6ccb713b 2018-01-19 stsp (*obj)->size = size;
585 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
586 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
587 b107e67f 2018-01-19 stsp
588 b107e67f 2018-01-19 stsp return NULL;
589 b107e67f 2018-01-19 stsp }
590 b107e67f 2018-01-19 stsp
591 b107e67f 2018-01-19 stsp static const struct got_error *
592 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
593 b107e67f 2018-01-19 stsp {
594 b107e67f 2018-01-19 stsp int64_t o = 0;
595 b107e67f 2018-01-19 stsp uint8_t offN;
596 b107e67f 2018-01-19 stsp size_t n;
597 b107e67f 2018-01-19 stsp int i = 0;
598 b107e67f 2018-01-19 stsp
599 b107e67f 2018-01-19 stsp do {
600 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
601 b107e67f 2018-01-19 stsp if (i > 8)
602 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
603 b107e67f 2018-01-19 stsp
604 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
605 b107e67f 2018-01-19 stsp if (n != 1)
606 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
607 b107e67f 2018-01-19 stsp
608 b107e67f 2018-01-19 stsp if (i == 0)
609 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
610 b107e67f 2018-01-19 stsp else {
611 cecc778e 2018-01-23 stsp o++;
612 b107e67f 2018-01-19 stsp o <<= 7;
613 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
614 b107e67f 2018-01-19 stsp }
615 b107e67f 2018-01-19 stsp i++;
616 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
617 6ccb713b 2018-01-19 stsp
618 b107e67f 2018-01-19 stsp *offset = o;
619 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
620 6ccb713b 2018-01-19 stsp return NULL;
621 6ccb713b 2018-01-19 stsp }
622 6ccb713b 2018-01-19 stsp
623 6ccb713b 2018-01-19 stsp static const struct got_error *
624 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
625 b107e67f 2018-01-19 stsp {
626 96f5e8b3 2018-01-23 stsp const struct got_error *err;
627 b107e67f 2018-01-19 stsp int64_t negoffset;
628 b107e67f 2018-01-19 stsp size_t negofflen;
629 b107e67f 2018-01-19 stsp
630 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
631 b107e67f 2018-01-19 stsp if (err)
632 b107e67f 2018-01-19 stsp return err;
633 b107e67f 2018-01-19 stsp
634 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
635 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
636 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
637 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
638 b107e67f 2018-01-19 stsp
639 96f5e8b3 2018-01-23 stsp return NULL;
640 96f5e8b3 2018-01-23 stsp }
641 96f5e8b3 2018-01-23 stsp
642 0e22967e 2018-02-11 stsp static const struct got_error *
643 0e22967e 2018-02-11 stsp resolve_delta_chain(struct got_delta_chain *, struct got_repository *,
644 0e22967e 2018-02-11 stsp FILE *, const char *, off_t, size_t, int, size_t);
645 a3500804 2018-01-23 stsp
646 96f5e8b3 2018-01-23 stsp static const struct got_error *
647 bdd2fbb3 2018-02-11 stsp add_delta(struct got_delta_chain *deltas, const char *path_packfile,
648 bdd2fbb3 2018-02-11 stsp off_t delta_offset, size_t tslen, int delta_type, size_t delta_size,
649 bdd2fbb3 2018-02-11 stsp size_t delta_data_offset)
650 bdd2fbb3 2018-02-11 stsp {
651 bdd2fbb3 2018-02-11 stsp struct got_delta *delta;
652 bdd2fbb3 2018-02-11 stsp
653 bdd2fbb3 2018-02-11 stsp delta = got_delta_open(path_packfile, delta_offset, tslen,
654 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
655 bdd2fbb3 2018-02-11 stsp if (delta == NULL)
656 bdd2fbb3 2018-02-11 stsp return got_error(GOT_ERR_NO_MEM);
657 bdd2fbb3 2018-02-11 stsp /* delta is freed in got_object_close() */
658 bdd2fbb3 2018-02-11 stsp deltas->nentries++;
659 bdd2fbb3 2018-02-11 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
660 bdd2fbb3 2018-02-11 stsp return NULL;
661 bdd2fbb3 2018-02-11 stsp }
662 bdd2fbb3 2018-02-11 stsp
663 bdd2fbb3 2018-02-11 stsp static const struct got_error *
664 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
665 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
666 bdd2fbb3 2018-02-11 stsp off_t delta_offset,size_t tslen, int delta_type, size_t delta_size)
667 bdd2fbb3 2018-02-11 stsp
668 a3500804 2018-01-23 stsp {
669 a3500804 2018-01-23 stsp const struct got_error *err;
670 c3703302 2018-01-23 stsp off_t base_offset;
671 c3703302 2018-01-23 stsp uint8_t base_type;
672 c3703302 2018-01-23 stsp uint64_t base_size;
673 c3703302 2018-01-23 stsp size_t base_tslen;
674 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
675 a3500804 2018-01-23 stsp
676 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
677 a3500804 2018-01-23 stsp if (err)
678 a3500804 2018-01-23 stsp return err;
679 a3500804 2018-01-23 stsp
680 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
681 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
682 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
683 bdd2fbb3 2018-02-11 stsp
684 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
685 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
686 bdd2fbb3 2018-02-11 stsp if (err)
687 bdd2fbb3 2018-02-11 stsp return err;
688 bdd2fbb3 2018-02-11 stsp
689 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
690 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
691 b107e67f 2018-01-19 stsp return got_error_from_errno();
692 b107e67f 2018-01-19 stsp
693 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
694 b107e67f 2018-01-19 stsp packfile);
695 b107e67f 2018-01-19 stsp if (err)
696 b107e67f 2018-01-19 stsp return err;
697 b107e67f 2018-01-19 stsp
698 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
699 0e22967e 2018-02-11 stsp base_offset, base_tslen, base_type, base_size);
700 c3703302 2018-01-23 stsp }
701 c3703302 2018-01-23 stsp
702 c3703302 2018-01-23 stsp static const struct got_error *
703 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
704 bdd2fbb3 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset,
705 bdd2fbb3 2018-02-11 stsp size_t tslen, int delta_type, size_t delta_size)
706 c3703302 2018-01-23 stsp {
707 6b9c9673 2018-01-23 stsp const struct got_error *err;
708 6b9c9673 2018-01-23 stsp struct got_object_id id;
709 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
710 6b9c9673 2018-01-23 stsp int idx;
711 6b9c9673 2018-01-23 stsp off_t base_offset;
712 6b9c9673 2018-01-23 stsp uint8_t base_type;
713 6b9c9673 2018-01-23 stsp uint64_t base_size;
714 6b9c9673 2018-01-23 stsp size_t base_tslen;
715 6b9c9673 2018-01-23 stsp size_t n;
716 6b9c9673 2018-01-23 stsp FILE *base_packfile;
717 6b9c9673 2018-01-23 stsp char *path_base_packfile;
718 bdd2fbb3 2018-02-11 stsp off_t delta_data_offset;
719 6b9c9673 2018-01-23 stsp
720 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
721 6b9c9673 2018-01-23 stsp if (n != 1)
722 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
723 6b9c9673 2018-01-23 stsp
724 bdd2fbb3 2018-02-11 stsp delta_data_offset = ftello(packfile);
725 bdd2fbb3 2018-02-11 stsp if (delta_data_offset == -1)
726 bdd2fbb3 2018-02-11 stsp return got_error_from_errno();
727 bdd2fbb3 2018-02-11 stsp
728 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
729 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, delta_data_offset);
730 bdd2fbb3 2018-02-11 stsp if (err)
731 bdd2fbb3 2018-02-11 stsp return err;
732 bdd2fbb3 2018-02-11 stsp
733 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
734 6b9c9673 2018-01-23 stsp if (err)
735 6b9c9673 2018-01-23 stsp return err;
736 6b9c9673 2018-01-23 stsp
737 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
738 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
739 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
740 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
741 6b9c9673 2018-01-23 stsp }
742 6b9c9673 2018-01-23 stsp
743 c7fe698a 2018-01-23 stsp err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
744 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
745 6b9c9673 2018-01-23 stsp if (err)
746 6b9c9673 2018-01-23 stsp return err;
747 6b9c9673 2018-01-23 stsp
748 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
749 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
750 6b9c9673 2018-01-23 stsp goto done;
751 6b9c9673 2018-01-23 stsp }
752 6b9c9673 2018-01-23 stsp
753 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
754 6b9c9673 2018-01-23 stsp base_packfile);
755 6b9c9673 2018-01-23 stsp if (err)
756 6b9c9673 2018-01-23 stsp goto done;
757 6b9c9673 2018-01-23 stsp
758 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
759 0e22967e 2018-02-11 stsp path_base_packfile, base_offset, base_tslen, base_type,
760 0e22967e 2018-02-11 stsp base_size);
761 6b9c9673 2018-01-23 stsp done:
762 6b9c9673 2018-01-23 stsp free(path_base_packfile);
763 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
764 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
765 6b9c9673 2018-01-23 stsp return err;
766 6b9c9673 2018-01-23 stsp }
767 6b9c9673 2018-01-23 stsp
768 6b9c9673 2018-01-23 stsp static const struct got_error *
769 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
770 0e22967e 2018-02-11 stsp FILE *packfile, const char *path_packfile, off_t delta_offset, size_t tslen,
771 0e22967e 2018-02-11 stsp int delta_type, size_t delta_size)
772 6b9c9673 2018-01-23 stsp {
773 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
774 c3703302 2018-01-23 stsp
775 c3703302 2018-01-23 stsp switch (delta_type) {
776 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
777 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
778 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
779 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
780 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
781 bdd2fbb3 2018-02-11 stsp err = add_delta(deltas, path_packfile, delta_offset, tslen,
782 bdd2fbb3 2018-02-11 stsp delta_type, delta_size, 0);
783 4e8cda55 2018-01-19 stsp break;
784 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
785 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
786 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
787 bdd2fbb3 2018-02-11 stsp delta_size);
788 96f5e8b3 2018-01-23 stsp break;
789 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
790 bdd2fbb3 2018-02-11 stsp err = resolve_ref_delta(deltas, repo, packfile,
791 bdd2fbb3 2018-02-11 stsp path_packfile, delta_offset, tslen, delta_type,
792 bdd2fbb3 2018-02-11 stsp delta_size);
793 6b9c9673 2018-01-23 stsp break;
794 4e8cda55 2018-01-19 stsp default:
795 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
796 4e8cda55 2018-01-19 stsp }
797 96f5e8b3 2018-01-23 stsp
798 96f5e8b3 2018-01-23 stsp return err;
799 96f5e8b3 2018-01-23 stsp }
800 96f5e8b3 2018-01-23 stsp
801 96f5e8b3 2018-01-23 stsp static const struct got_error *
802 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
803 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
804 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
805 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
806 96f5e8b3 2018-01-23 stsp {
807 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
808 96f5e8b3 2018-01-23 stsp int resolved_type;
809 4e8cda55 2018-01-19 stsp
810 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
811 b107e67f 2018-01-19 stsp if (*obj == NULL)
812 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
813 b107e67f 2018-01-19 stsp
814 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
815 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
816 39e73dc9 2018-03-03 stsp (*obj)->size = 0; /* Not known because deltas aren't applied yet. */
817 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
818 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
819 b107e67f 2018-01-19 stsp
820 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
821 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
822 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
823 96f5e8b3 2018-01-23 stsp goto done;
824 96f5e8b3 2018-01-23 stsp }
825 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
826 96f5e8b3 2018-01-23 stsp
827 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
828 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
829 c3703302 2018-01-23 stsp
830 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
831 0e22967e 2018-02-11 stsp path_packfile, offset, tslen, delta_type, delta_size);
832 96f5e8b3 2018-01-23 stsp if (err)
833 96f5e8b3 2018-01-23 stsp goto done;
834 96f5e8b3 2018-01-23 stsp
835 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
836 96f5e8b3 2018-01-23 stsp if (err)
837 96f5e8b3 2018-01-23 stsp goto done;
838 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
839 96f5e8b3 2018-01-23 stsp
840 96f5e8b3 2018-01-23 stsp done:
841 96f5e8b3 2018-01-23 stsp if (err) {
842 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
843 96f5e8b3 2018-01-23 stsp *obj = NULL;
844 96f5e8b3 2018-01-23 stsp }
845 96f5e8b3 2018-01-23 stsp return err;
846 b107e67f 2018-01-19 stsp }
847 b107e67f 2018-01-19 stsp
848 b107e67f 2018-01-19 stsp static const struct got_error *
849 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
850 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
851 a1fd68d8 2018-01-12 stsp {
852 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
853 a1fd68d8 2018-01-12 stsp off_t offset;
854 6c00b545 2018-01-17 stsp char *path_packfile;
855 6c00b545 2018-01-17 stsp FILE *packfile;
856 6c00b545 2018-01-17 stsp uint8_t type;
857 6c00b545 2018-01-17 stsp uint64_t size;
858 3ee5fc21 2018-01-17 stsp size_t tslen;
859 a1fd68d8 2018-01-12 stsp
860 6c00b545 2018-01-17 stsp *obj = NULL;
861 a1fd68d8 2018-01-12 stsp
862 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
863 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
864 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
865 a1fd68d8 2018-01-12 stsp
866 c7fe698a 2018-01-23 stsp err = open_packfile(&packfile, &path_packfile, repo, packidx);
867 6b9c9673 2018-01-23 stsp if (err)
868 6b9c9673 2018-01-23 stsp return err;
869 a1fd68d8 2018-01-12 stsp
870 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
871 6c00b545 2018-01-17 stsp err = got_error_from_errno();
872 6c00b545 2018-01-17 stsp goto done;
873 6c00b545 2018-01-17 stsp }
874 6c00b545 2018-01-17 stsp
875 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
876 a1fd68d8 2018-01-12 stsp if (err)
877 a1fd68d8 2018-01-12 stsp goto done;
878 a1fd68d8 2018-01-12 stsp
879 6c00b545 2018-01-17 stsp switch (type) {
880 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
881 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
882 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
883 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
884 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
885 6ccb713b 2018-01-19 stsp offset + tslen, size);
886 6c00b545 2018-01-17 stsp break;
887 6ccb713b 2018-01-19 stsp
888 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
889 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
890 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
891 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
892 b107e67f 2018-01-19 stsp break;
893 b107e67f 2018-01-19 stsp
894 6c00b545 2018-01-17 stsp default:
895 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
896 6c00b545 2018-01-17 stsp goto done;
897 6c00b545 2018-01-17 stsp }
898 a1fd68d8 2018-01-12 stsp done:
899 a1fd68d8 2018-01-12 stsp free(path_packfile);
900 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
901 f334529e 2018-01-12 stsp err = got_error_from_errno();
902 a1fd68d8 2018-01-12 stsp return err;
903 a1fd68d8 2018-01-12 stsp }
904 a1fd68d8 2018-01-12 stsp
905 a1fd68d8 2018-01-12 stsp const struct got_error *
906 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
907 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
908 a1fd68d8 2018-01-12 stsp {
909 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
910 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
911 6b9c9673 2018-01-23 stsp int idx;
912 a1fd68d8 2018-01-12 stsp
913 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
914 6b9c9673 2018-01-23 stsp if (err)
915 6b9c9673 2018-01-23 stsp return err;
916 a1fd68d8 2018-01-12 stsp
917 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
918 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
919 3ee5fc21 2018-01-17 stsp return err;
920 3ee5fc21 2018-01-17 stsp }
921 efd2a263 2018-01-19 stsp
922 efd2a263 2018-01-19 stsp static const struct got_error *
923 710bb5ca 2018-01-23 stsp dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
924 efd2a263 2018-01-19 stsp {
925 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
926 6691714a 2018-01-23 stsp struct got_delta *delta;
927 6691714a 2018-01-23 stsp FILE *base_file, *accum_file;
928 6691714a 2018-01-23 stsp int n = 0;
929 3ee5fc21 2018-01-17 stsp
930 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
931 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
932 efd2a263 2018-01-19 stsp
933 6691714a 2018-01-23 stsp base_file = got_opentemp();
934 6691714a 2018-01-23 stsp if (base_file == NULL)
935 6691714a 2018-01-23 stsp return got_error_from_errno();
936 efd2a263 2018-01-19 stsp
937 6691714a 2018-01-23 stsp accum_file = got_opentemp();
938 6691714a 2018-01-23 stsp if (accum_file == NULL) {
939 6691714a 2018-01-23 stsp err = got_error_from_errno();
940 6691714a 2018-01-23 stsp fclose(base_file);
941 efd2a263 2018-01-19 stsp return err;
942 6691714a 2018-01-23 stsp }
943 efd2a263 2018-01-19 stsp
944 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
945 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
946 885d3e02 2018-01-27 stsp uint8_t *delta_buf = NULL;
947 885d3e02 2018-01-27 stsp size_t delta_len = 0;
948 885d3e02 2018-01-27 stsp FILE *delta_file;
949 885d3e02 2018-01-27 stsp
950 885d3e02 2018-01-27 stsp delta_file = fopen(delta->path_packfile, "rb");
951 6691714a 2018-01-23 stsp if (delta_file == NULL) {
952 6691714a 2018-01-23 stsp err = got_error_from_errno();
953 6691714a 2018-01-23 stsp goto done;
954 6691714a 2018-01-23 stsp }
955 6691714a 2018-01-23 stsp
956 a6b158cc 2018-02-11 stsp
957 a6b158cc 2018-02-11 stsp if (n == 0) {
958 a6b158cc 2018-02-11 stsp /* Plain object types are the delta base. */
959 a6b158cc 2018-02-11 stsp if (delta->type != GOT_OBJ_TYPE_COMMIT &&
960 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TREE &&
961 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_BLOB &&
962 a6b158cc 2018-02-11 stsp delta->type != GOT_OBJ_TYPE_TAG) {
963 a6b158cc 2018-02-11 stsp err = got_error(GOT_ERR_BAD_DELTA_CHAIN);
964 a6b158cc 2018-02-11 stsp goto done;
965 a6b158cc 2018-02-11 stsp }
966 6691714a 2018-01-23 stsp
967 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->offset + delta->tslen,
968 bdd2fbb3 2018-02-11 stsp SEEK_SET) != 0) {
969 bdd2fbb3 2018-02-11 stsp fclose(delta_file);
970 bdd2fbb3 2018-02-11 stsp err = got_error_from_errno();
971 bdd2fbb3 2018-02-11 stsp goto done;
972 bdd2fbb3 2018-02-11 stsp }
973 a6b158cc 2018-02-11 stsp err = got_inflate_to_file(&delta_len, delta_file,
974 a6b158cc 2018-02-11 stsp base_file);
975 a6b158cc 2018-02-11 stsp fclose(delta_file);
976 a6b158cc 2018-02-11 stsp if (err)
977 a6b158cc 2018-02-11 stsp goto done;
978 a6b158cc 2018-02-11 stsp n++;
979 a6b158cc 2018-02-11 stsp rewind(base_file);
980 a6b158cc 2018-02-11 stsp continue;
981 a6b158cc 2018-02-11 stsp }
982 a6b158cc 2018-02-11 stsp
983 bdd2fbb3 2018-02-11 stsp if (fseeko(delta_file, delta->data_offset, SEEK_CUR) != 0) {
984 0e22967e 2018-02-11 stsp fclose(delta_file);
985 0e22967e 2018-02-11 stsp err = got_error_from_errno();
986 0e22967e 2018-02-11 stsp goto done;
987 0e22967e 2018-02-11 stsp }
988 0e22967e 2018-02-11 stsp
989 885d3e02 2018-01-27 stsp /* Delta streams should always fit in memory. */
990 61d262a8 2018-02-11 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
991 a6b158cc 2018-02-11 stsp fclose(delta_file);
992 885d3e02 2018-01-27 stsp if (err)
993 a6b158cc 2018-02-11 stsp goto done;
994 885d3e02 2018-01-27 stsp
995 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, delta_buf, delta_len,
996 6691714a 2018-01-23 stsp /* Final delta application writes to the output file. */
997 710bb5ca 2018-01-23 stsp ++n < deltas->nentries ? accum_file : outfile);
998 885d3e02 2018-01-27 stsp free(delta_buf);
999 6691714a 2018-01-23 stsp if (err)
1000 6691714a 2018-01-23 stsp goto done;
1001 6691714a 2018-01-23 stsp
1002 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
1003 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
1004 6691714a 2018-01-23 stsp FILE *tmp = accum_file;
1005 6691714a 2018-01-23 stsp accum_file = base_file;
1006 6691714a 2018-01-23 stsp base_file = tmp;
1007 6691714a 2018-01-23 stsp rewind(base_file);
1008 6691714a 2018-01-23 stsp rewind(accum_file);
1009 6691714a 2018-01-23 stsp }
1010 6691714a 2018-01-23 stsp }
1011 6691714a 2018-01-23 stsp
1012 6691714a 2018-01-23 stsp done:
1013 6691714a 2018-01-23 stsp fclose(base_file);
1014 6691714a 2018-01-23 stsp fclose(accum_file);
1015 6691714a 2018-01-23 stsp rewind(outfile);
1016 efd2a263 2018-01-19 stsp return err;
1017 efd2a263 2018-01-19 stsp }
1018 efd2a263 2018-01-19 stsp
1019 3ee5fc21 2018-01-17 stsp const struct got_error *
1020 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
1021 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
1022 3ee5fc21 2018-01-17 stsp {
1023 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
1024 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
1025 3ee5fc21 2018-01-17 stsp
1026 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
1027 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
1028 3ee5fc21 2018-01-17 stsp
1029 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
1030 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
1031 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
1032 3ee5fc21 2018-01-17 stsp goto done;
1033 3ee5fc21 2018-01-17 stsp }
1034 3ee5fc21 2018-01-17 stsp
1035 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
1036 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
1037 6691714a 2018-01-23 stsp if (packfile == NULL) {
1038 6691714a 2018-01-23 stsp err = got_error_from_errno();
1039 6691714a 2018-01-23 stsp goto done;
1040 6691714a 2018-01-23 stsp }
1041 3ee5fc21 2018-01-17 stsp
1042 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
1043 6691714a 2018-01-23 stsp err = got_error_from_errno();
1044 6691714a 2018-01-23 stsp goto done;
1045 6691714a 2018-01-23 stsp }
1046 3ee5fc21 2018-01-17 stsp
1047 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&obj->size, packfile, *f);
1048 6691714a 2018-01-23 stsp } else
1049 710bb5ca 2018-01-23 stsp err = dump_delta_chain(&obj->deltas, *f);
1050 3ee5fc21 2018-01-17 stsp done:
1051 3ee5fc21 2018-01-17 stsp if (packfile)
1052 3ee5fc21 2018-01-17 stsp fclose(packfile);
1053 3ee5fc21 2018-01-17 stsp if (err && *f)
1054 3ee5fc21 2018-01-17 stsp fclose(*f);
1055 a1fd68d8 2018-01-12 stsp return err;
1056 a1fd68d8 2018-01-12 stsp }