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 a1fd68d8 2018-01-12 stsp #include "got_sha1.h"
36 0a0a3048 2018-01-10 stsp #include "pack.h"
37 a1fd68d8 2018-01-12 stsp #include "path.h"
38 efd2a263 2018-01-19 stsp #include "delta.h"
39 eef6493a 2018-01-19 stsp #include "object.h"
40 885d3e02 2018-01-27 stsp #include "zb.h"
41 0a0a3048 2018-01-10 stsp
42 a1fd68d8 2018-01-12 stsp #define GOT_PACK_PREFIX "pack-"
43 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_SUFFIX ".pack"
44 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_SUFFIX ".idx"
45 a1fd68d8 2018-01-12 stsp #define GOT_PACKFILE_NAMELEN (strlen(GOT_PACK_PREFIX) + \
46 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
47 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKFILE_SUFFIX))
48 a1fd68d8 2018-01-12 stsp #define GOT_PACKIDX_NAMELEN (strlen(GOT_PACK_PREFIX) + \
49 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1 + \
50 a1fd68d8 2018-01-12 stsp strlen(GOT_PACKIDX_SUFFIX))
51 a1fd68d8 2018-01-12 stsp
52 a1fd68d8 2018-01-12 stsp #ifndef MIN
53 a1fd68d8 2018-01-12 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 a1fd68d8 2018-01-12 stsp #endif
55 a1fd68d8 2018-01-12 stsp
56 0a0a3048 2018-01-10 stsp static const struct got_error *
57 0a0a3048 2018-01-10 stsp verify_fanout_table(uint32_t *fanout_table)
58 0a0a3048 2018-01-10 stsp {
59 0a0a3048 2018-01-10 stsp int i;
60 0a0a3048 2018-01-10 stsp
61 0a0a3048 2018-01-10 stsp for (i = 0; i < 0xff - 1; i++) {
62 a1fd68d8 2018-01-12 stsp if (be32toh(fanout_table[i]) > be32toh(fanout_table[i + 1]))
63 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PACKIDX);
64 0a0a3048 2018-01-10 stsp }
65 0a0a3048 2018-01-10 stsp
66 0a0a3048 2018-01-10 stsp return NULL;
67 0a0a3048 2018-01-10 stsp }
68 0a0a3048 2018-01-10 stsp
69 24541888 2018-01-10 stsp static const struct got_error *
70 0a0a3048 2018-01-10 stsp get_packfile_size(size_t *size, const char *path_idx)
71 0a0a3048 2018-01-10 stsp {
72 0a0a3048 2018-01-10 stsp struct stat sb;
73 0a0a3048 2018-01-10 stsp char *path_pack;
74 0a0a3048 2018-01-10 stsp char base_path[PATH_MAX];
75 0a0a3048 2018-01-10 stsp char *dot;
76 0a0a3048 2018-01-10 stsp
77 0a0a3048 2018-01-10 stsp if (strlcpy(base_path, path_idx, PATH_MAX) > PATH_MAX)
78 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_SPACE);
79 0a0a3048 2018-01-10 stsp
80 0a0a3048 2018-01-10 stsp dot = strrchr(base_path, '.');
81 0a0a3048 2018-01-10 stsp if (dot == NULL)
82 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
83 0a0a3048 2018-01-10 stsp *dot = '\0';
84 0a0a3048 2018-01-10 stsp if (asprintf(&path_pack, "%s.pack", base_path) == -1)
85 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_NO_MEM);
86 0a0a3048 2018-01-10 stsp
87 0a0a3048 2018-01-10 stsp if (stat(path_pack, &sb) != 0) {
88 0a0a3048 2018-01-10 stsp free(path_pack);
89 8251fdbc 2018-01-12 stsp return got_error_from_errno();
90 0a0a3048 2018-01-10 stsp }
91 0a0a3048 2018-01-10 stsp
92 0a0a3048 2018-01-10 stsp free(path_pack);
93 0a0a3048 2018-01-10 stsp *size = sb.st_size;
94 0a0a3048 2018-01-10 stsp return 0;
95 0a0a3048 2018-01-10 stsp }
96 0a0a3048 2018-01-10 stsp
97 0a0a3048 2018-01-10 stsp const struct got_error *
98 0a0a3048 2018-01-10 stsp got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
99 0a0a3048 2018-01-10 stsp {
100 0a0a3048 2018-01-10 stsp struct got_packidx_v2_hdr *p;
101 0a0a3048 2018-01-10 stsp FILE *f;
102 0a0a3048 2018-01-10 stsp const struct got_error *err = NULL;
103 0a0a3048 2018-01-10 stsp size_t n, nobj, packfile_size;
104 0ebaf008 2018-01-10 stsp SHA1_CTX ctx;
105 0ebaf008 2018-01-10 stsp uint8_t sha1[SHA1_DIGEST_LENGTH];
106 0a0a3048 2018-01-10 stsp
107 0ebaf008 2018-01-10 stsp SHA1Init(&ctx);
108 0ebaf008 2018-01-10 stsp
109 0a0a3048 2018-01-10 stsp f = fopen(path, "rb");
110 0a0a3048 2018-01-10 stsp if (f == NULL)
111 0a0a3048 2018-01-10 stsp return got_error(GOT_ERR_BAD_PATH);
112 0a0a3048 2018-01-10 stsp
113 0a0a3048 2018-01-10 stsp err = get_packfile_size(&packfile_size, path);
114 0a0a3048 2018-01-10 stsp if (err)
115 0a0a3048 2018-01-10 stsp return err;
116 0a0a3048 2018-01-10 stsp
117 0a0a3048 2018-01-10 stsp p = calloc(1, sizeof(*p));
118 0a0a3048 2018-01-10 stsp if (p == NULL) {
119 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
120 0a0a3048 2018-01-10 stsp goto done;
121 0a0a3048 2018-01-10 stsp }
122 0a0a3048 2018-01-10 stsp
123 0a0a3048 2018-01-10 stsp n = fread(&p->magic, sizeof(p->magic), 1, f);
124 0a0a3048 2018-01-10 stsp if (n != 1) {
125 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
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 if (betoh32(p->magic) != GOT_PACKIDX_V2_MAGIC) {
130 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
131 0a0a3048 2018-01-10 stsp goto done;
132 0a0a3048 2018-01-10 stsp }
133 0a0a3048 2018-01-10 stsp
134 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->magic, sizeof(p->magic));
135 0ebaf008 2018-01-10 stsp
136 0a0a3048 2018-01-10 stsp n = fread(&p->version, sizeof(p->version), 1, f);
137 0a0a3048 2018-01-10 stsp if (n != 1) {
138 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
139 0a0a3048 2018-01-10 stsp goto done;
140 0a0a3048 2018-01-10 stsp }
141 0a0a3048 2018-01-10 stsp
142 0a0a3048 2018-01-10 stsp if (betoh32(p->version) != GOT_PACKIDX_VERSION) {
143 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_BAD_PACKIDX);
144 0a0a3048 2018-01-10 stsp goto done;
145 0a0a3048 2018-01-10 stsp }
146 0a0a3048 2018-01-10 stsp
147 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)&p->version, sizeof(p->version));
148 0ebaf008 2018-01-10 stsp
149 0a0a3048 2018-01-10 stsp n = fread(&p->fanout_table, sizeof(p->fanout_table), 1, f);
150 0a0a3048 2018-01-10 stsp if (n != 1) {
151 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
152 0a0a3048 2018-01-10 stsp goto done;
153 0a0a3048 2018-01-10 stsp }
154 0a0a3048 2018-01-10 stsp
155 0a0a3048 2018-01-10 stsp err = verify_fanout_table(p->fanout_table);
156 0a0a3048 2018-01-10 stsp if (err)
157 0a0a3048 2018-01-10 stsp goto done;
158 0a0a3048 2018-01-10 stsp
159 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->fanout_table, sizeof(p->fanout_table));
160 0ebaf008 2018-01-10 stsp
161 0a0a3048 2018-01-10 stsp nobj = betoh32(p->fanout_table[0xff]);
162 0a0a3048 2018-01-10 stsp
163 0a0a3048 2018-01-10 stsp p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
164 0a0a3048 2018-01-10 stsp if (p->sorted_ids == NULL) {
165 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
166 0a0a3048 2018-01-10 stsp goto done;
167 0a0a3048 2018-01-10 stsp }
168 0a0a3048 2018-01-10 stsp
169 0a0a3048 2018-01-10 stsp n = fread(p->sorted_ids, sizeof(*p->sorted_ids), nobj, f);
170 0a0a3048 2018-01-10 stsp if (n != nobj) {
171 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
172 0a0a3048 2018-01-10 stsp goto done;
173 0a0a3048 2018-01-10 stsp }
174 0a0a3048 2018-01-10 stsp
175 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t *)p->sorted_ids,
176 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->sorted_ids));
177 0ebaf008 2018-01-10 stsp
178 a1fd68d8 2018-01-12 stsp p->crc32 = calloc(nobj, sizeof(*p->crc32));
179 a1fd68d8 2018-01-12 stsp if (p->crc32 == NULL) {
180 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
181 0a0a3048 2018-01-10 stsp goto done;
182 0a0a3048 2018-01-10 stsp }
183 0a0a3048 2018-01-10 stsp
184 a1fd68d8 2018-01-12 stsp n = fread(p->crc32, sizeof(*p->crc32), nobj, f);
185 0a0a3048 2018-01-10 stsp if (n != nobj) {
186 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
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 SHA1Update(&ctx, (uint8_t *)p->crc32, nobj * sizeof(*p->crc32));
191 0ebaf008 2018-01-10 stsp
192 a1fd68d8 2018-01-12 stsp p->offsets = calloc(nobj, sizeof(*p->offsets));
193 a1fd68d8 2018-01-12 stsp if (p->offsets == NULL) {
194 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
195 0a0a3048 2018-01-10 stsp goto done;
196 0a0a3048 2018-01-10 stsp }
197 0a0a3048 2018-01-10 stsp
198 a1fd68d8 2018-01-12 stsp n = fread(p->offsets, sizeof(*p->offsets), nobj, f);
199 0a0a3048 2018-01-10 stsp if (n != nobj) {
200 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
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 SHA1Update(&ctx, (uint8_t *)p->offsets, nobj * sizeof(*p->offsets));
205 0ebaf008 2018-01-10 stsp
206 0a0a3048 2018-01-10 stsp /* Large file offsets are contained only in files > 2GB. */
207 b0517dd0 2018-01-10 stsp if (packfile_size <= 0x80000000)
208 0a0a3048 2018-01-10 stsp goto checksum;
209 0a0a3048 2018-01-10 stsp
210 0a0a3048 2018-01-10 stsp p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
211 0a0a3048 2018-01-10 stsp if (p->large_offsets == NULL) {
212 0a0a3048 2018-01-10 stsp err = got_error(GOT_ERR_NO_MEM);
213 0a0a3048 2018-01-10 stsp goto done;
214 0a0a3048 2018-01-10 stsp }
215 0a0a3048 2018-01-10 stsp
216 0a0a3048 2018-01-10 stsp n = fread(p->large_offsets, sizeof(*p->large_offsets), nobj, f);
217 0a0a3048 2018-01-10 stsp if (n != nobj) {
218 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
219 0a0a3048 2018-01-10 stsp goto done;
220 0a0a3048 2018-01-10 stsp }
221 0a0a3048 2018-01-10 stsp
222 0ebaf008 2018-01-10 stsp SHA1Update(&ctx, (uint8_t*)p->large_offsets,
223 0ebaf008 2018-01-10 stsp nobj * sizeof(*p->large_offsets));
224 0ebaf008 2018-01-10 stsp
225 0a0a3048 2018-01-10 stsp checksum:
226 0a0a3048 2018-01-10 stsp n = fread(&p->trailer, sizeof(p->trailer), 1, f);
227 0a0a3048 2018-01-10 stsp if (n != 1) {
228 8251fdbc 2018-01-12 stsp err = got_ferror(f, GOT_ERR_BAD_PACKIDX);
229 0a0a3048 2018-01-10 stsp goto done;
230 0a0a3048 2018-01-10 stsp }
231 0a0a3048 2018-01-10 stsp
232 a1fd68d8 2018-01-12 stsp SHA1Update(&ctx, p->trailer.packfile_sha1, SHA1_DIGEST_LENGTH);
233 0ebaf008 2018-01-10 stsp SHA1Final(sha1, &ctx);
234 a1fd68d8 2018-01-12 stsp if (memcmp(p->trailer.packidx_sha1, sha1, SHA1_DIGEST_LENGTH) != 0)
235 0ebaf008 2018-01-10 stsp err = got_error(GOT_ERR_PACKIDX_CSUM);
236 0a0a3048 2018-01-10 stsp done:
237 0a0a3048 2018-01-10 stsp fclose(f);
238 0a0a3048 2018-01-10 stsp if (err)
239 0a0a3048 2018-01-10 stsp got_packidx_close(p);
240 0a0a3048 2018-01-10 stsp else
241 0a0a3048 2018-01-10 stsp *packidx = p;
242 0a0a3048 2018-01-10 stsp return err;
243 0a0a3048 2018-01-10 stsp }
244 0a0a3048 2018-01-10 stsp
245 0a0a3048 2018-01-10 stsp void
246 0a0a3048 2018-01-10 stsp got_packidx_close(struct got_packidx_v2_hdr *packidx)
247 0a0a3048 2018-01-10 stsp {
248 0a0a3048 2018-01-10 stsp free(packidx->sorted_ids);
249 0a0a3048 2018-01-10 stsp free(packidx->offsets);
250 0a0a3048 2018-01-10 stsp free(packidx->crc32);
251 0a0a3048 2018-01-10 stsp free(packidx->large_offsets);
252 0a0a3048 2018-01-10 stsp free(packidx);
253 a1fd68d8 2018-01-12 stsp }
254 a1fd68d8 2018-01-12 stsp
255 a1fd68d8 2018-01-12 stsp static int
256 a1fd68d8 2018-01-12 stsp is_packidx_filename(const char *name, size_t len)
257 a1fd68d8 2018-01-12 stsp {
258 a1fd68d8 2018-01-12 stsp if (len != GOT_PACKIDX_NAMELEN)
259 a1fd68d8 2018-01-12 stsp return 0;
260 a1fd68d8 2018-01-12 stsp
261 a1fd68d8 2018-01-12 stsp if (strncmp(name, GOT_PACK_PREFIX, strlen(GOT_PACK_PREFIX)) != 0)
262 a1fd68d8 2018-01-12 stsp return 0;
263 a1fd68d8 2018-01-12 stsp
264 a1fd68d8 2018-01-12 stsp if (strcmp(name + strlen(GOT_PACK_PREFIX) +
265 a1fd68d8 2018-01-12 stsp SHA1_DIGEST_STRING_LENGTH - 1, GOT_PACKIDX_SUFFIX) != 0)
266 a1fd68d8 2018-01-12 stsp return 0;
267 a1fd68d8 2018-01-12 stsp
268 a1fd68d8 2018-01-12 stsp return 1;
269 a1fd68d8 2018-01-12 stsp }
270 a1fd68d8 2018-01-12 stsp
271 a1fd68d8 2018-01-12 stsp static off_t
272 a1fd68d8 2018-01-12 stsp get_object_offset(struct got_packidx_v2_hdr *packidx, int idx)
273 a1fd68d8 2018-01-12 stsp {
274 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
275 a1fd68d8 2018-01-12 stsp uint32_t offset = betoh32(packidx->offsets[idx]);
276 a1fd68d8 2018-01-12 stsp if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
277 a1fd68d8 2018-01-12 stsp uint64_t loffset;
278 a1fd68d8 2018-01-12 stsp idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
279 a1fd68d8 2018-01-12 stsp if (idx < 0 || idx > totobj || packidx->large_offsets == NULL)
280 a1fd68d8 2018-01-12 stsp return -1;
281 a1fd68d8 2018-01-12 stsp loffset = betoh64(packidx->large_offsets[idx]);
282 a1fd68d8 2018-01-12 stsp return (loffset > INT64_MAX ? -1 : (off_t)loffset);
283 a1fd68d8 2018-01-12 stsp }
284 a1fd68d8 2018-01-12 stsp return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
285 a1fd68d8 2018-01-12 stsp }
286 a1fd68d8 2018-01-12 stsp
287 a1fd68d8 2018-01-12 stsp static int
288 a1fd68d8 2018-01-12 stsp get_object_idx(struct got_packidx_v2_hdr *packidx, struct got_object_id *id)
289 a1fd68d8 2018-01-12 stsp {
290 a1fd68d8 2018-01-12 stsp u_int8_t id0 = id->sha1[0];
291 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
292 a1fd68d8 2018-01-12 stsp int i = 0;
293 a1fd68d8 2018-01-12 stsp
294 a1fd68d8 2018-01-12 stsp if (id0 > 0)
295 a1fd68d8 2018-01-12 stsp i = betoh32(packidx->fanout_table[id0 - 1]);
296 a1fd68d8 2018-01-12 stsp
297 a1fd68d8 2018-01-12 stsp while (i < totobj) {
298 6c00b545 2018-01-17 stsp struct got_object_id *oid = &packidx->sorted_ids[i];
299 a1fd68d8 2018-01-12 stsp uint32_t offset;
300 2b2ca9f0 2018-01-13 stsp int cmp = got_object_id_cmp(id, oid);
301 a1fd68d8 2018-01-12 stsp
302 6c00b545 2018-01-17 stsp if (cmp == 0)
303 6c00b545 2018-01-17 stsp return i;
304 6c00b545 2018-01-17 stsp else if (cmp > 0)
305 a1fd68d8 2018-01-12 stsp break;
306 6c00b545 2018-01-17 stsp i++;
307 a1fd68d8 2018-01-12 stsp }
308 a1fd68d8 2018-01-12 stsp
309 a1fd68d8 2018-01-12 stsp return -1;
310 6b9c9673 2018-01-23 stsp }
311 6b9c9673 2018-01-23 stsp
312 6b9c9673 2018-01-23 stsp static const struct got_error *
313 6b9c9673 2018-01-23 stsp search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
314 6b9c9673 2018-01-23 stsp struct got_repository *repo, struct got_object_id *id)
315 6b9c9673 2018-01-23 stsp {
316 6b9c9673 2018-01-23 stsp const struct got_error *err;
317 6b9c9673 2018-01-23 stsp char *path_packdir;
318 6b9c9673 2018-01-23 stsp DIR *packdir;
319 6b9c9673 2018-01-23 stsp struct dirent *dent;
320 6b9c9673 2018-01-23 stsp char *path_packidx;
321 6b9c9673 2018-01-23 stsp
322 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
323 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
324 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
325 6b9c9673 2018-01-23 stsp
326 6b9c9673 2018-01-23 stsp packdir = opendir(path_packdir);
327 6b9c9673 2018-01-23 stsp if (packdir == NULL) {
328 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
329 6b9c9673 2018-01-23 stsp goto done;
330 6b9c9673 2018-01-23 stsp }
331 6b9c9673 2018-01-23 stsp
332 6b9c9673 2018-01-23 stsp while ((dent = readdir(packdir)) != NULL) {
333 6b9c9673 2018-01-23 stsp if (!is_packidx_filename(dent->d_name, dent->d_namlen))
334 6b9c9673 2018-01-23 stsp continue;
335 6b9c9673 2018-01-23 stsp
336 6b9c9673 2018-01-23 stsp if (asprintf(&path_packidx, "%s/%s", path_packdir,
337 6b9c9673 2018-01-23 stsp dent->d_name) == -1) {
338 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
339 6b9c9673 2018-01-23 stsp goto done;
340 6b9c9673 2018-01-23 stsp }
341 6b9c9673 2018-01-23 stsp
342 6b9c9673 2018-01-23 stsp err = got_packidx_open(packidx, path_packidx);
343 6b9c9673 2018-01-23 stsp free(path_packidx);
344 6b9c9673 2018-01-23 stsp if (err)
345 6b9c9673 2018-01-23 stsp goto done;
346 6b9c9673 2018-01-23 stsp
347 6b9c9673 2018-01-23 stsp *idx = get_object_idx(*packidx, id);
348 6b9c9673 2018-01-23 stsp if (*idx != -1) {
349 6b9c9673 2018-01-23 stsp err = NULL; /* found the object */
350 6b9c9673 2018-01-23 stsp goto done;
351 6b9c9673 2018-01-23 stsp }
352 6b9c9673 2018-01-23 stsp
353 6b9c9673 2018-01-23 stsp got_packidx_close(*packidx);
354 6b9c9673 2018-01-23 stsp *packidx = NULL;
355 6b9c9673 2018-01-23 stsp }
356 6b9c9673 2018-01-23 stsp
357 6b9c9673 2018-01-23 stsp err = got_error(GOT_ERR_NO_OBJ);
358 6b9c9673 2018-01-23 stsp done:
359 6b9c9673 2018-01-23 stsp free(path_packdir);
360 ef715580 2018-01-26 stsp if (packdir && closedir(packdir) != 0 && err == 0)
361 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
362 6b9c9673 2018-01-23 stsp return err;
363 6b9c9673 2018-01-23 stsp }
364 6b9c9673 2018-01-23 stsp
365 c7fe698a 2018-01-23 stsp static const struct got_error *
366 6b9c9673 2018-01-23 stsp get_packfile_path(char **path_packfile, struct got_repository *repo,
367 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx)
368 6b9c9673 2018-01-23 stsp {
369 6b9c9673 2018-01-23 stsp char *path_packdir;
370 6b9c9673 2018-01-23 stsp char hex[SHA1_DIGEST_STRING_LENGTH];
371 6b9c9673 2018-01-23 stsp char *sha1str;
372 6b9c9673 2018-01-23 stsp char *path_packidx;
373 6b9c9673 2018-01-23 stsp
374 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
375 6b9c9673 2018-01-23 stsp
376 6b9c9673 2018-01-23 stsp path_packdir = got_repo_get_path_objects_pack(repo);
377 6b9c9673 2018-01-23 stsp if (path_packdir == NULL)
378 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
379 6b9c9673 2018-01-23 stsp
380 6b9c9673 2018-01-23 stsp sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
381 6b9c9673 2018-01-23 stsp hex, sizeof(hex));
382 6b9c9673 2018-01-23 stsp if (sha1str == NULL)
383 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_PACKIDX_CSUM);
384 6b9c9673 2018-01-23 stsp
385 6b9c9673 2018-01-23 stsp if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
386 6b9c9673 2018-01-23 stsp GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
387 6b9c9673 2018-01-23 stsp *path_packfile = NULL;
388 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
389 6b9c9673 2018-01-23 stsp }
390 6b9c9673 2018-01-23 stsp
391 6b9c9673 2018-01-23 stsp return NULL;
392 a1fd68d8 2018-01-12 stsp }
393 a1fd68d8 2018-01-12 stsp
394 a1fd68d8 2018-01-12 stsp const struct got_error *
395 a1fd68d8 2018-01-12 stsp read_packfile_hdr(FILE *f, struct got_packidx_v2_hdr *packidx)
396 a1fd68d8 2018-01-12 stsp {
397 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
398 a1fd68d8 2018-01-12 stsp uint32_t totobj = betoh32(packidx->fanout_table[0xff]);
399 a1fd68d8 2018-01-12 stsp struct got_packfile_hdr hdr;
400 a1fd68d8 2018-01-12 stsp size_t n;
401 a1fd68d8 2018-01-12 stsp
402 a1fd68d8 2018-01-12 stsp n = fread(&hdr, sizeof(hdr), 1, f);
403 a1fd68d8 2018-01-12 stsp if (n != 1)
404 8251fdbc 2018-01-12 stsp return got_ferror(f, GOT_ERR_BAD_PACKIDX);
405 a1fd68d8 2018-01-12 stsp
406 a1fd68d8 2018-01-12 stsp if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
407 a1fd68d8 2018-01-12 stsp betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
408 a1fd68d8 2018-01-12 stsp betoh32(hdr.nobjects) != totobj)
409 a1fd68d8 2018-01-12 stsp err = got_error(GOT_ERR_BAD_PACKFILE);
410 a1fd68d8 2018-01-12 stsp
411 a1fd68d8 2018-01-12 stsp return err;
412 a487c1d0 2018-01-14 stsp }
413 a487c1d0 2018-01-14 stsp
414 a487c1d0 2018-01-14 stsp static const struct got_error *
415 c7fe698a 2018-01-23 stsp open_packfile(FILE **packfile, char **path_packfile,
416 c7fe698a 2018-01-23 stsp struct got_repository *repo, struct got_packidx_v2_hdr *packidx)
417 c7fe698a 2018-01-23 stsp {
418 c7fe698a 2018-01-23 stsp const struct got_error *err;
419 c7fe698a 2018-01-23 stsp
420 c7fe698a 2018-01-23 stsp *packfile = NULL;
421 c7fe698a 2018-01-23 stsp
422 c7fe698a 2018-01-23 stsp err = get_packfile_path(path_packfile, repo, packidx);
423 c7fe698a 2018-01-23 stsp if (err)
424 c7fe698a 2018-01-23 stsp return err;
425 c7fe698a 2018-01-23 stsp
426 c7fe698a 2018-01-23 stsp *packfile = fopen(*path_packfile, "rb");
427 c7fe698a 2018-01-23 stsp if (*packfile == NULL) {
428 c7fe698a 2018-01-23 stsp err = got_error_from_errno();
429 c7fe698a 2018-01-23 stsp free(*path_packfile);
430 c7fe698a 2018-01-23 stsp return err;
431 c7fe698a 2018-01-23 stsp }
432 c7fe698a 2018-01-23 stsp
433 c7fe698a 2018-01-23 stsp err = read_packfile_hdr(*packfile, packidx);
434 c7fe698a 2018-01-23 stsp if (err) {
435 c7fe698a 2018-01-23 stsp fclose(*packfile);
436 c7fe698a 2018-01-23 stsp *packfile = NULL;
437 c7fe698a 2018-01-23 stsp }
438 c7fe698a 2018-01-23 stsp return err;
439 c7fe698a 2018-01-23 stsp }
440 c7fe698a 2018-01-23 stsp
441 c7fe698a 2018-01-23 stsp static const struct got_error *
442 348f621c 2018-01-23 stsp parse_object_type_and_size(uint8_t *type, uint64_t *size, size_t *len,
443 c3703302 2018-01-23 stsp FILE *packfile)
444 a487c1d0 2018-01-14 stsp {
445 a487c1d0 2018-01-14 stsp uint8_t t = 0;
446 a487c1d0 2018-01-14 stsp uint64_t s = 0;
447 a487c1d0 2018-01-14 stsp uint8_t sizeN;
448 a487c1d0 2018-01-14 stsp size_t n;
449 a487c1d0 2018-01-14 stsp int i = 0;
450 a487c1d0 2018-01-14 stsp
451 a1fd68d8 2018-01-12 stsp do {
452 a1fd68d8 2018-01-12 stsp /* We do not support size values which don't fit in 64 bit. */
453 a487c1d0 2018-01-14 stsp if (i > 9)
454 a487c1d0 2018-01-14 stsp return got_error(GOT_ERR_NO_SPACE);
455 a1fd68d8 2018-01-12 stsp
456 a1fd68d8 2018-01-12 stsp n = fread(&sizeN, sizeof(sizeN), 1, packfile);
457 a487c1d0 2018-01-14 stsp if (n != 1)
458 a487c1d0 2018-01-14 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
459 8251fdbc 2018-01-12 stsp
460 a1fd68d8 2018-01-12 stsp if (i == 0) {
461 a487c1d0 2018-01-14 stsp t = (sizeN & GOT_PACK_OBJ_SIZE0_TYPE_MASK) >>
462 a1fd68d8 2018-01-12 stsp GOT_PACK_OBJ_SIZE0_TYPE_MASK_SHIFT;
463 a487c1d0 2018-01-14 stsp s = (sizeN & GOT_PACK_OBJ_SIZE0_VAL_MASK);
464 a1fd68d8 2018-01-12 stsp } else {
465 a1fd68d8 2018-01-12 stsp size_t shift = 4 + 7 * (i - 1);
466 a487c1d0 2018-01-14 stsp s |= ((sizeN & GOT_PACK_OBJ_SIZE_VAL_MASK) << shift);
467 a1fd68d8 2018-01-12 stsp }
468 a1fd68d8 2018-01-12 stsp i++;
469 a1fd68d8 2018-01-12 stsp } while (sizeN & GOT_PACK_OBJ_SIZE_MORE);
470 a1fd68d8 2018-01-12 stsp
471 a487c1d0 2018-01-14 stsp *type = t;
472 a487c1d0 2018-01-14 stsp *size = s;
473 3ee5fc21 2018-01-17 stsp *len = i * sizeof(sizeN);
474 a487c1d0 2018-01-14 stsp return NULL;
475 0a0a3048 2018-01-10 stsp }
476 c54542a0 2018-01-13 stsp
477 a1fd68d8 2018-01-12 stsp static const struct got_error *
478 9710aac2 2018-01-19 stsp open_plain_object(struct got_object **obj, const char *path_packfile,
479 6ccb713b 2018-01-19 stsp struct got_object_id *id, uint8_t type, off_t offset, size_t size)
480 6ccb713b 2018-01-19 stsp {
481 6ccb713b 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
482 6ccb713b 2018-01-19 stsp if (*obj == NULL)
483 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
484 6ccb713b 2018-01-19 stsp
485 6ccb713b 2018-01-19 stsp (*obj)->path_packfile = strdup(path_packfile);
486 6ccb713b 2018-01-19 stsp if ((*obj)->path_packfile == NULL) {
487 6ccb713b 2018-01-19 stsp free(*obj);
488 6ccb713b 2018-01-19 stsp *obj = NULL;
489 6ccb713b 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
490 6ccb713b 2018-01-19 stsp }
491 6ccb713b 2018-01-19 stsp
492 6ccb713b 2018-01-19 stsp (*obj)->type = type;
493 6ccb713b 2018-01-19 stsp (*obj)->flags = GOT_OBJ_FLAG_PACKED;
494 6ccb713b 2018-01-19 stsp (*obj)->hdrlen = 0;
495 6ccb713b 2018-01-19 stsp (*obj)->size = size;
496 6ccb713b 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
497 6ccb713b 2018-01-19 stsp (*obj)->pack_offset = offset;
498 b107e67f 2018-01-19 stsp
499 b107e67f 2018-01-19 stsp return NULL;
500 b107e67f 2018-01-19 stsp }
501 b107e67f 2018-01-19 stsp
502 b107e67f 2018-01-19 stsp static const struct got_error *
503 348f621c 2018-01-23 stsp parse_negative_offset(int64_t *offset, size_t *len, FILE *packfile)
504 b107e67f 2018-01-19 stsp {
505 b107e67f 2018-01-19 stsp int64_t o = 0;
506 b107e67f 2018-01-19 stsp uint8_t offN;
507 b107e67f 2018-01-19 stsp size_t n;
508 b107e67f 2018-01-19 stsp int i = 0;
509 b107e67f 2018-01-19 stsp
510 b107e67f 2018-01-19 stsp do {
511 b107e67f 2018-01-19 stsp /* We do not support offset values which don't fit in 64 bit. */
512 b107e67f 2018-01-19 stsp if (i > 8)
513 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_SPACE);
514 b107e67f 2018-01-19 stsp
515 b107e67f 2018-01-19 stsp n = fread(&offN, sizeof(offN), 1, packfile);
516 b107e67f 2018-01-19 stsp if (n != 1)
517 b107e67f 2018-01-19 stsp return got_ferror(packfile, GOT_ERR_BAD_PACKIDX);
518 b107e67f 2018-01-19 stsp
519 b107e67f 2018-01-19 stsp if (i == 0)
520 b107e67f 2018-01-19 stsp o = (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
521 b107e67f 2018-01-19 stsp else {
522 cecc778e 2018-01-23 stsp o++;
523 b107e67f 2018-01-19 stsp o <<= 7;
524 cecc778e 2018-01-23 stsp o += (offN & GOT_PACK_OBJ_DELTA_OFF_VAL_MASK);
525 b107e67f 2018-01-19 stsp }
526 b107e67f 2018-01-19 stsp i++;
527 b107e67f 2018-01-19 stsp } while (offN & GOT_PACK_OBJ_DELTA_OFF_MORE);
528 6ccb713b 2018-01-19 stsp
529 b107e67f 2018-01-19 stsp *offset = o;
530 b107e67f 2018-01-19 stsp *len = i * sizeof(offN);
531 6ccb713b 2018-01-19 stsp return NULL;
532 6ccb713b 2018-01-19 stsp }
533 6ccb713b 2018-01-19 stsp
534 6ccb713b 2018-01-19 stsp static const struct got_error *
535 96f5e8b3 2018-01-23 stsp parse_offset_delta(off_t *base_offset, FILE *packfile, off_t offset)
536 b107e67f 2018-01-19 stsp {
537 96f5e8b3 2018-01-23 stsp const struct got_error *err;
538 b107e67f 2018-01-19 stsp int64_t negoffset;
539 b107e67f 2018-01-19 stsp size_t negofflen;
540 b107e67f 2018-01-19 stsp
541 348f621c 2018-01-23 stsp err = parse_negative_offset(&negoffset, &negofflen, packfile);
542 b107e67f 2018-01-19 stsp if (err)
543 b107e67f 2018-01-19 stsp return err;
544 b107e67f 2018-01-19 stsp
545 b107e67f 2018-01-19 stsp /* Compute the base object's offset (must be in the same pack file). */
546 96f5e8b3 2018-01-23 stsp *base_offset = (offset - negoffset);
547 96f5e8b3 2018-01-23 stsp if (*base_offset <= 0)
548 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_BAD_PACKFILE);
549 b107e67f 2018-01-19 stsp
550 96f5e8b3 2018-01-23 stsp return NULL;
551 96f5e8b3 2018-01-23 stsp }
552 96f5e8b3 2018-01-23 stsp
553 a3500804 2018-01-23 stsp static const struct got_error *resolve_delta_chain(struct got_delta_chain *,
554 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *, const char *, int, off_t, size_t);
555 a3500804 2018-01-23 stsp
556 96f5e8b3 2018-01-23 stsp static const struct got_error *
557 6b9c9673 2018-01-23 stsp resolve_offset_delta(struct got_delta_chain *deltas,
558 6b9c9673 2018-01-23 stsp struct got_repository *repo, FILE *packfile, const char *path_packfile,
559 6b9c9673 2018-01-23 stsp off_t delta_offset)
560 a3500804 2018-01-23 stsp {
561 a3500804 2018-01-23 stsp const struct got_error *err;
562 c3703302 2018-01-23 stsp off_t base_offset;
563 c3703302 2018-01-23 stsp uint8_t base_type;
564 c3703302 2018-01-23 stsp uint64_t base_size;
565 c3703302 2018-01-23 stsp size_t base_tslen;
566 a3500804 2018-01-23 stsp
567 c3703302 2018-01-23 stsp err = parse_offset_delta(&base_offset, packfile, delta_offset);
568 a3500804 2018-01-23 stsp if (err)
569 a3500804 2018-01-23 stsp return err;
570 a3500804 2018-01-23 stsp
571 c3703302 2018-01-23 stsp /* An offset delta must be in the same packfile. */
572 c3703302 2018-01-23 stsp if (fseeko(packfile, base_offset, SEEK_SET) != 0)
573 b107e67f 2018-01-19 stsp return got_error_from_errno();
574 b107e67f 2018-01-19 stsp
575 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
576 b107e67f 2018-01-19 stsp packfile);
577 b107e67f 2018-01-19 stsp if (err)
578 b107e67f 2018-01-19 stsp return err;
579 b107e67f 2018-01-19 stsp
580 6b9c9673 2018-01-23 stsp return resolve_delta_chain(deltas, repo, packfile, path_packfile,
581 c3703302 2018-01-23 stsp base_type, base_offset + base_tslen, base_size);
582 c3703302 2018-01-23 stsp }
583 c3703302 2018-01-23 stsp
584 c3703302 2018-01-23 stsp static const struct got_error *
585 6b9c9673 2018-01-23 stsp resolve_ref_delta(struct got_delta_chain *deltas, struct got_repository *repo,
586 6b9c9673 2018-01-23 stsp FILE *packfile, const char *path_packfile, off_t delta_offset)
587 c3703302 2018-01-23 stsp {
588 6b9c9673 2018-01-23 stsp const struct got_error *err;
589 6b9c9673 2018-01-23 stsp struct got_object_id id;
590 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx;
591 6b9c9673 2018-01-23 stsp int idx;
592 6b9c9673 2018-01-23 stsp off_t base_offset;
593 6b9c9673 2018-01-23 stsp uint8_t base_type;
594 6b9c9673 2018-01-23 stsp uint64_t base_size;
595 6b9c9673 2018-01-23 stsp size_t base_tslen;
596 6b9c9673 2018-01-23 stsp size_t n;
597 6b9c9673 2018-01-23 stsp FILE *base_packfile;
598 6b9c9673 2018-01-23 stsp char *path_base_packfile;
599 6b9c9673 2018-01-23 stsp
600 6b9c9673 2018-01-23 stsp n = fread(&id, sizeof(id), 1, packfile);
601 6b9c9673 2018-01-23 stsp if (n != 1)
602 6b9c9673 2018-01-23 stsp return got_ferror(packfile, GOT_ERR_IO);
603 6b9c9673 2018-01-23 stsp
604 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, &id);
605 6b9c9673 2018-01-23 stsp if (err)
606 6b9c9673 2018-01-23 stsp return err;
607 6b9c9673 2018-01-23 stsp
608 6b9c9673 2018-01-23 stsp base_offset = get_object_offset(packidx, idx);
609 6b9c9673 2018-01-23 stsp if (base_offset == (uint64_t)-1) {
610 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
611 6b9c9673 2018-01-23 stsp return got_error(GOT_ERR_BAD_PACKIDX);
612 6b9c9673 2018-01-23 stsp }
613 6b9c9673 2018-01-23 stsp
614 c7fe698a 2018-01-23 stsp err = open_packfile(&base_packfile, &path_base_packfile, repo, packidx);
615 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
616 6b9c9673 2018-01-23 stsp if (err)
617 6b9c9673 2018-01-23 stsp return err;
618 6b9c9673 2018-01-23 stsp
619 6b9c9673 2018-01-23 stsp if (fseeko(base_packfile, base_offset, SEEK_SET) != 0) {
620 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
621 6b9c9673 2018-01-23 stsp goto done;
622 6b9c9673 2018-01-23 stsp }
623 6b9c9673 2018-01-23 stsp
624 6b9c9673 2018-01-23 stsp err = parse_object_type_and_size(&base_type, &base_size, &base_tslen,
625 6b9c9673 2018-01-23 stsp base_packfile);
626 6b9c9673 2018-01-23 stsp if (err)
627 6b9c9673 2018-01-23 stsp goto done;
628 6b9c9673 2018-01-23 stsp
629 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(deltas, repo, base_packfile,
630 6b9c9673 2018-01-23 stsp path_base_packfile, base_type, base_offset + base_tslen, base_size);
631 6b9c9673 2018-01-23 stsp done:
632 6b9c9673 2018-01-23 stsp free(path_base_packfile);
633 6b9c9673 2018-01-23 stsp if (base_packfile && fclose(base_packfile) == -1 && err == 0)
634 6b9c9673 2018-01-23 stsp err = got_error_from_errno();
635 6b9c9673 2018-01-23 stsp return err;
636 6b9c9673 2018-01-23 stsp }
637 6b9c9673 2018-01-23 stsp
638 6b9c9673 2018-01-23 stsp static const struct got_error *
639 6b9c9673 2018-01-23 stsp resolve_delta_chain(struct got_delta_chain *deltas, struct got_repository *repo,
640 6b9c9673 2018-01-23 stsp FILE *packfile, const char *path_packfile, int delta_type,
641 6b9c9673 2018-01-23 stsp off_t delta_offset, size_t delta_size)
642 6b9c9673 2018-01-23 stsp {
643 c3703302 2018-01-23 stsp const struct got_error *err = NULL;
644 c3703302 2018-01-23 stsp struct got_delta *delta;
645 c3703302 2018-01-23 stsp
646 c3703302 2018-01-23 stsp delta = got_delta_open(path_packfile, delta_type, delta_offset,
647 96f5e8b3 2018-01-23 stsp delta_size);
648 c3703302 2018-01-23 stsp if (delta == NULL)
649 96f5e8b3 2018-01-23 stsp return got_error(GOT_ERR_NO_MEM);
650 96f5e8b3 2018-01-23 stsp deltas->nentries++;
651 6691714a 2018-01-23 stsp SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
652 c3703302 2018-01-23 stsp /* In case of error below, delta is freed in got_object_close(). */
653 96f5e8b3 2018-01-23 stsp
654 c3703302 2018-01-23 stsp switch (delta_type) {
655 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_COMMIT:
656 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TREE:
657 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_BLOB:
658 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_TAG:
659 c3703302 2018-01-23 stsp /* Plain types are the final delta base. Recursion ends. */
660 4e8cda55 2018-01-19 stsp break;
661 a3500804 2018-01-23 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
662 6b9c9673 2018-01-23 stsp err = resolve_offset_delta(deltas, repo, packfile,
663 6b9c9673 2018-01-23 stsp path_packfile, delta_offset);
664 96f5e8b3 2018-01-23 stsp break;
665 4e8cda55 2018-01-19 stsp case GOT_OBJ_TYPE_REF_DELTA:
666 6b9c9673 2018-01-23 stsp err = resolve_ref_delta(deltas, repo, packfile, path_packfile,
667 6b9c9673 2018-01-23 stsp delta_offset);
668 6b9c9673 2018-01-23 stsp break;
669 4e8cda55 2018-01-19 stsp default:
670 4e8cda55 2018-01-19 stsp return got_error(GOT_ERR_NOT_IMPL);
671 4e8cda55 2018-01-19 stsp }
672 96f5e8b3 2018-01-23 stsp
673 96f5e8b3 2018-01-23 stsp return err;
674 96f5e8b3 2018-01-23 stsp }
675 96f5e8b3 2018-01-23 stsp
676 96f5e8b3 2018-01-23 stsp static const struct got_error *
677 a48db7e5 2018-01-23 stsp open_delta_object(struct got_object **obj, struct got_repository *repo,
678 a48db7e5 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, const char *path_packfile,
679 a48db7e5 2018-01-23 stsp FILE *packfile, struct got_object_id *id, off_t offset, size_t tslen,
680 a48db7e5 2018-01-23 stsp int delta_type, size_t delta_size)
681 96f5e8b3 2018-01-23 stsp {
682 96f5e8b3 2018-01-23 stsp const struct got_error *err = NULL;
683 96f5e8b3 2018-01-23 stsp struct got_object_id base_id;
684 96f5e8b3 2018-01-23 stsp uint8_t base_type;
685 96f5e8b3 2018-01-23 stsp int resolved_type;
686 96f5e8b3 2018-01-23 stsp uint64_t base_size;
687 96f5e8b3 2018-01-23 stsp size_t base_tslen;
688 4e8cda55 2018-01-19 stsp
689 b107e67f 2018-01-19 stsp *obj = calloc(1, sizeof(**obj));
690 b107e67f 2018-01-19 stsp if (*obj == NULL)
691 b107e67f 2018-01-19 stsp return got_error(GOT_ERR_NO_MEM);
692 b107e67f 2018-01-19 stsp
693 96f5e8b3 2018-01-23 stsp (*obj)->flags = 0;
694 b107e67f 2018-01-19 stsp (*obj)->hdrlen = 0;
695 96f5e8b3 2018-01-23 stsp (*obj)->size = 0; /* Not yet known because deltas aren't combined. */
696 b107e67f 2018-01-19 stsp memcpy(&(*obj)->id, id, sizeof((*obj)->id));
697 cecc778e 2018-01-23 stsp (*obj)->pack_offset = offset + tslen;
698 b107e67f 2018-01-19 stsp
699 96f5e8b3 2018-01-23 stsp (*obj)->path_packfile = strdup(path_packfile);
700 96f5e8b3 2018-01-23 stsp if ((*obj)->path_packfile == NULL) {
701 96f5e8b3 2018-01-23 stsp err = got_error(GOT_ERR_NO_MEM);
702 96f5e8b3 2018-01-23 stsp goto done;
703 96f5e8b3 2018-01-23 stsp }
704 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_PACKED;
705 96f5e8b3 2018-01-23 stsp
706 96f5e8b3 2018-01-23 stsp SIMPLEQ_INIT(&(*obj)->deltas.entries);
707 96f5e8b3 2018-01-23 stsp (*obj)->flags |= GOT_OBJ_FLAG_DELTIFIED;
708 c3703302 2018-01-23 stsp
709 6b9c9673 2018-01-23 stsp err = resolve_delta_chain(&(*obj)->deltas, repo, packfile,
710 a48db7e5 2018-01-23 stsp path_packfile, delta_type, offset, delta_size);
711 96f5e8b3 2018-01-23 stsp if (err)
712 96f5e8b3 2018-01-23 stsp goto done;
713 96f5e8b3 2018-01-23 stsp
714 96f5e8b3 2018-01-23 stsp err = got_delta_chain_get_base_type(&resolved_type, &(*obj)->deltas);
715 96f5e8b3 2018-01-23 stsp if (err)
716 96f5e8b3 2018-01-23 stsp goto done;
717 96f5e8b3 2018-01-23 stsp (*obj)->type = resolved_type;
718 96f5e8b3 2018-01-23 stsp
719 96f5e8b3 2018-01-23 stsp done:
720 96f5e8b3 2018-01-23 stsp if (err) {
721 96f5e8b3 2018-01-23 stsp got_object_close(*obj);
722 96f5e8b3 2018-01-23 stsp *obj = NULL;
723 96f5e8b3 2018-01-23 stsp }
724 96f5e8b3 2018-01-23 stsp return err;
725 b107e67f 2018-01-19 stsp }
726 b107e67f 2018-01-19 stsp
727 b107e67f 2018-01-19 stsp static const struct got_error *
728 6c00b545 2018-01-17 stsp open_packed_object(struct got_object **obj, struct got_repository *repo,
729 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx, int idx, struct got_object_id *id)
730 a1fd68d8 2018-01-12 stsp {
731 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
732 a1fd68d8 2018-01-12 stsp off_t offset;
733 6c00b545 2018-01-17 stsp char *path_packfile;
734 6c00b545 2018-01-17 stsp FILE *packfile;
735 6c00b545 2018-01-17 stsp uint8_t type;
736 6c00b545 2018-01-17 stsp uint64_t size;
737 3ee5fc21 2018-01-17 stsp size_t tslen;
738 a1fd68d8 2018-01-12 stsp
739 6c00b545 2018-01-17 stsp *obj = NULL;
740 a1fd68d8 2018-01-12 stsp
741 a1fd68d8 2018-01-12 stsp offset = get_object_offset(packidx, idx);
742 a1fd68d8 2018-01-12 stsp if (offset == (uint64_t)-1)
743 a1fd68d8 2018-01-12 stsp return got_error(GOT_ERR_BAD_PACKIDX);
744 a1fd68d8 2018-01-12 stsp
745 c7fe698a 2018-01-23 stsp err = open_packfile(&packfile, &path_packfile, repo, packidx);
746 6b9c9673 2018-01-23 stsp if (err)
747 6b9c9673 2018-01-23 stsp return err;
748 a1fd68d8 2018-01-12 stsp
749 6c00b545 2018-01-17 stsp if (fseeko(packfile, offset, SEEK_SET) != 0) {
750 6c00b545 2018-01-17 stsp err = got_error_from_errno();
751 6c00b545 2018-01-17 stsp goto done;
752 6c00b545 2018-01-17 stsp }
753 6c00b545 2018-01-17 stsp
754 348f621c 2018-01-23 stsp err = parse_object_type_and_size(&type, &size, &tslen, packfile);
755 a1fd68d8 2018-01-12 stsp if (err)
756 a1fd68d8 2018-01-12 stsp goto done;
757 a1fd68d8 2018-01-12 stsp
758 6c00b545 2018-01-17 stsp switch (type) {
759 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_COMMIT:
760 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_TREE:
761 6c00b545 2018-01-17 stsp case GOT_OBJ_TYPE_BLOB:
762 d33fc9ef 2018-01-23 stsp case GOT_OBJ_TYPE_TAG:
763 9710aac2 2018-01-19 stsp err = open_plain_object(obj, path_packfile, id, type,
764 6ccb713b 2018-01-19 stsp offset + tslen, size);
765 6c00b545 2018-01-17 stsp break;
766 6ccb713b 2018-01-19 stsp
767 b107e67f 2018-01-19 stsp case GOT_OBJ_TYPE_OFFSET_DELTA:
768 a48db7e5 2018-01-23 stsp case GOT_OBJ_TYPE_REF_DELTA:
769 a48db7e5 2018-01-23 stsp err = open_delta_object(obj, repo, packidx, path_packfile,
770 a48db7e5 2018-01-23 stsp packfile, id, offset, tslen, type, size);
771 b107e67f 2018-01-19 stsp break;
772 b107e67f 2018-01-19 stsp
773 6c00b545 2018-01-17 stsp default:
774 6c00b545 2018-01-17 stsp err = got_error(GOT_ERR_NOT_IMPL);
775 6c00b545 2018-01-17 stsp goto done;
776 6c00b545 2018-01-17 stsp }
777 a1fd68d8 2018-01-12 stsp done:
778 a1fd68d8 2018-01-12 stsp free(path_packfile);
779 f334529e 2018-01-12 stsp if (packfile && fclose(packfile) == -1 && err == 0)
780 f334529e 2018-01-12 stsp err = got_error_from_errno();
781 a1fd68d8 2018-01-12 stsp return err;
782 a1fd68d8 2018-01-12 stsp }
783 a1fd68d8 2018-01-12 stsp
784 a1fd68d8 2018-01-12 stsp const struct got_error *
785 6c00b545 2018-01-17 stsp got_packfile_open_object(struct got_object **obj, struct got_object_id *id,
786 a1fd68d8 2018-01-12 stsp struct got_repository *repo)
787 a1fd68d8 2018-01-12 stsp {
788 a1fd68d8 2018-01-12 stsp const struct got_error *err = NULL;
789 6b9c9673 2018-01-23 stsp struct got_packidx_v2_hdr *packidx = NULL;
790 6b9c9673 2018-01-23 stsp int idx;
791 a1fd68d8 2018-01-12 stsp
792 6b9c9673 2018-01-23 stsp err = search_packidx(&packidx, &idx, repo, id);
793 6b9c9673 2018-01-23 stsp if (err)
794 6b9c9673 2018-01-23 stsp return err;
795 a1fd68d8 2018-01-12 stsp
796 6b9c9673 2018-01-23 stsp err = open_packed_object(obj, repo, packidx, idx, id);
797 6b9c9673 2018-01-23 stsp got_packidx_close(packidx);
798 3ee5fc21 2018-01-17 stsp return err;
799 3ee5fc21 2018-01-17 stsp }
800 efd2a263 2018-01-19 stsp
801 efd2a263 2018-01-19 stsp static const struct got_error *
802 710bb5ca 2018-01-23 stsp dump_delta_chain(struct got_delta_chain *deltas, FILE *outfile)
803 efd2a263 2018-01-19 stsp {
804 efd2a263 2018-01-19 stsp const struct got_error *err = NULL;
805 6691714a 2018-01-23 stsp struct got_delta *delta;
806 6691714a 2018-01-23 stsp FILE *base_file, *accum_file;
807 6691714a 2018-01-23 stsp int n = 0;
808 3ee5fc21 2018-01-17 stsp
809 710bb5ca 2018-01-23 stsp if (SIMPLEQ_EMPTY(&deltas->entries))
810 6691714a 2018-01-23 stsp return got_error(GOT_ERR_BAD_DELTA_CHAIN);
811 efd2a263 2018-01-19 stsp
812 6691714a 2018-01-23 stsp base_file = got_opentemp();
813 6691714a 2018-01-23 stsp if (base_file == NULL)
814 6691714a 2018-01-23 stsp return got_error_from_errno();
815 efd2a263 2018-01-19 stsp
816 6691714a 2018-01-23 stsp accum_file = got_opentemp();
817 6691714a 2018-01-23 stsp if (accum_file == NULL) {
818 6691714a 2018-01-23 stsp err = got_error_from_errno();
819 6691714a 2018-01-23 stsp fclose(base_file);
820 efd2a263 2018-01-19 stsp return err;
821 6691714a 2018-01-23 stsp }
822 efd2a263 2018-01-19 stsp
823 6691714a 2018-01-23 stsp /* Deltas are ordered in ascending order. */
824 710bb5ca 2018-01-23 stsp SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
825 885d3e02 2018-01-27 stsp uint8_t *delta_buf = NULL;
826 885d3e02 2018-01-27 stsp size_t delta_len = 0;
827 885d3e02 2018-01-27 stsp FILE *delta_file;
828 885d3e02 2018-01-27 stsp
829 885d3e02 2018-01-27 stsp delta_file = fopen(delta->path_packfile, "rb");
830 6691714a 2018-01-23 stsp if (delta_file == NULL) {
831 6691714a 2018-01-23 stsp err = got_error_from_errno();
832 6691714a 2018-01-23 stsp goto done;
833 6691714a 2018-01-23 stsp }
834 6691714a 2018-01-23 stsp
835 6691714a 2018-01-23 stsp if (fseeko(delta_file, delta->offset, SEEK_SET) != 0) {
836 6691714a 2018-01-23 stsp fclose(delta_file);
837 6691714a 2018-01-23 stsp err = got_error_from_errno();
838 6691714a 2018-01-23 stsp goto done;
839 6691714a 2018-01-23 stsp }
840 6691714a 2018-01-23 stsp
841 885d3e02 2018-01-27 stsp /* Delta streams should always fit in memory. */
842 61d262a8 2018-02-11 stsp err = got_inflate_to_mem(&delta_buf, &delta_len, delta_file);
843 885d3e02 2018-01-27 stsp if (err)
844 885d3e02 2018-01-27 stsp return err;
845 885d3e02 2018-01-27 stsp
846 885d3e02 2018-01-27 stsp fclose(delta_file);
847 885d3e02 2018-01-27 stsp
848 885d3e02 2018-01-27 stsp err = got_delta_apply(base_file, delta_buf, delta_len,
849 6691714a 2018-01-23 stsp /* Final delta application writes to the output file. */
850 710bb5ca 2018-01-23 stsp ++n < deltas->nentries ? accum_file : outfile);
851 885d3e02 2018-01-27 stsp free(delta_buf);
852 6691714a 2018-01-23 stsp if (err)
853 6691714a 2018-01-23 stsp goto done;
854 6691714a 2018-01-23 stsp
855 710bb5ca 2018-01-23 stsp if (n < deltas->nentries) {
856 6691714a 2018-01-23 stsp /* Accumulated delta becomes the new base. */
857 6691714a 2018-01-23 stsp FILE *tmp = accum_file;
858 6691714a 2018-01-23 stsp accum_file = base_file;
859 6691714a 2018-01-23 stsp base_file = tmp;
860 6691714a 2018-01-23 stsp rewind(base_file);
861 6691714a 2018-01-23 stsp rewind(accum_file);
862 6691714a 2018-01-23 stsp }
863 6691714a 2018-01-23 stsp }
864 6691714a 2018-01-23 stsp
865 6691714a 2018-01-23 stsp done:
866 6691714a 2018-01-23 stsp fclose(base_file);
867 6691714a 2018-01-23 stsp fclose(accum_file);
868 6691714a 2018-01-23 stsp rewind(outfile);
869 efd2a263 2018-01-19 stsp return err;
870 efd2a263 2018-01-19 stsp }
871 efd2a263 2018-01-19 stsp
872 3ee5fc21 2018-01-17 stsp const struct got_error *
873 3ee5fc21 2018-01-17 stsp got_packfile_extract_object(FILE **f, struct got_object *obj,
874 3ee5fc21 2018-01-17 stsp struct got_repository *repo)
875 3ee5fc21 2018-01-17 stsp {
876 3ee5fc21 2018-01-17 stsp const struct got_error *err = NULL;
877 3ee5fc21 2018-01-17 stsp FILE *packfile = NULL;
878 3ee5fc21 2018-01-17 stsp
879 3ee5fc21 2018-01-17 stsp if ((obj->flags & GOT_OBJ_FLAG_PACKED) == 0)
880 3ee5fc21 2018-01-17 stsp return got_error(GOT_ERR_OBJ_NOT_PACKED);
881 3ee5fc21 2018-01-17 stsp
882 3ee5fc21 2018-01-17 stsp *f = got_opentemp();
883 3ee5fc21 2018-01-17 stsp if (*f == NULL) {
884 3ee5fc21 2018-01-17 stsp err = got_error(GOT_ERR_FILE_OPEN);
885 3ee5fc21 2018-01-17 stsp goto done;
886 3ee5fc21 2018-01-17 stsp }
887 3ee5fc21 2018-01-17 stsp
888 6691714a 2018-01-23 stsp if ((obj->flags & GOT_OBJ_FLAG_DELTIFIED) == 0) {
889 6691714a 2018-01-23 stsp packfile = fopen(obj->path_packfile, "rb");
890 6691714a 2018-01-23 stsp if (packfile == NULL) {
891 6691714a 2018-01-23 stsp err = got_error_from_errno();
892 6691714a 2018-01-23 stsp goto done;
893 6691714a 2018-01-23 stsp }
894 3ee5fc21 2018-01-17 stsp
895 6691714a 2018-01-23 stsp if (fseeko(packfile, obj->pack_offset, SEEK_SET) != 0) {
896 6691714a 2018-01-23 stsp err = got_error_from_errno();
897 6691714a 2018-01-23 stsp goto done;
898 6691714a 2018-01-23 stsp }
899 3ee5fc21 2018-01-17 stsp
900 3606d7fc 2018-02-11 stsp err = got_inflate_to_file(&obj->size, packfile, *f);
901 6691714a 2018-01-23 stsp } else
902 710bb5ca 2018-01-23 stsp err = dump_delta_chain(&obj->deltas, *f);
903 3ee5fc21 2018-01-17 stsp done:
904 3ee5fc21 2018-01-17 stsp if (packfile)
905 3ee5fc21 2018-01-17 stsp fclose(packfile);
906 3ee5fc21 2018-01-17 stsp if (err && *f)
907 3ee5fc21 2018-01-17 stsp fclose(*f);
908 a1fd68d8 2018-01-12 stsp return err;
909 a1fd68d8 2018-01-12 stsp }