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