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